Skip to content

Commit

Permalink
feat: add client
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
  • Loading branch information
vishal-chdhry committed Oct 8, 2024
1 parent 244c964 commit 632f09d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
86 changes: 84 additions & 2 deletions pkg/storage/etcd/new.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,91 @@
package etcd

func NewETCDClient() {
import (
"errors"
"time"

reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
"github.com/kyverno/reports-server/pkg/storage/api"
"github.com/kyverno/reports-server/pkg/utils"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/embed"
"k8s.io/klog/v2"
"sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2"
)

var (
etcdEndpoints = embed.DefaultListenClientURLs
dialTimeout = 10 * time.Second
)

type etcdClient struct {
polrClient ObjectStorageNamespaced[*v1alpha2.PolicyReport]
ephrClient ObjectStorageNamespaced[*reportsv1.EphemeralReport]
cpolrClient ObjectStorageCluster[*v1alpha2.ClusterPolicyReport]
cephrClient ObjectStorageCluster[*reportsv1.ClusterEphemeralReport]
}

func NewETCDServer() {
func New() (api.Storage, error) {
client, err := clientv3.New(clientv3.Config{
DialTimeout: dialTimeout,
Endpoints: []string{etcdEndpoints},
})

Check warning on line 32 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L28-L32

Added lines #L28 - L32 were not covered by tests

if err != nil {
return nil, err

Check warning on line 35 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

return &etcdClient{
polrClient: NewObjectStoreNamespaced[*v1alpha2.PolicyReport](client, utils.PolicyReportsGVK, utils.PolicyReportsGR),
ephrClient: NewObjectStoreNamespaced[*reportsv1.EphemeralReport](client, utils.EphemeralReportsGVK, utils.EphemeralReportsGR),
cpolrClient: NewObjectStoreCluster[*v1alpha2.ClusterPolicyReport](client, utils.ClusterPolicyReportsGVK, utils.ClusterPolicyReportsGR),
cephrClient: NewObjectStoreCluster[*reportsv1.ClusterEphemeralReport](client, utils.ClusterEphemeralReportsGVK, utils.ClusterEphemeralReportsGR),
}, nil

Check warning on line 43 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L38-L43

Added lines #L38 - L43 were not covered by tests
}

func (e *etcdClient) Ready() bool {
return true

Check warning on line 47 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (e *etcdClient) PolicyReports() api.PolicyReportsInterface {
return e.polrClient

Check warning on line 51 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

func (e *etcdClient) ClusterPolicyReports() api.ClusterPolicyReportsInterface {
return e.cpolrClient

Check warning on line 55 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}

func (e *etcdClient) EphemeralReports() api.EphemeralReportsInterface {
return e.ephrClient

Check warning on line 59 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}

func (e *etcdClient) ClusterEphemeralReports() api.ClusterEphemeralReportsInterface {
return e.cephrClient

Check warning on line 63 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}

func StartETCDServer(stopCh <-chan struct{}, dir string) error {
etcdConfig := embed.NewConfig()
etcdConfig.Dir = dir
etcd, err := embed.StartEtcd(etcdConfig)
if err != nil {
return err

Check warning on line 71 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L66-L71

Added lines #L66 - L71 were not covered by tests
}
defer etcd.Close()

Check warning on line 73 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L73

Added line #L73 was not covered by tests

select {
case <-etcd.Server.ReadyNotify():
klog.Info("etcd server is running!")
case <-time.After(100 * time.Second):
etcd.Server.Stop()
return errors.New("etcd server timed out and stopped!")

Check warning on line 80 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L75-L80

Added lines #L75 - L80 were not covered by tests
}

select {
case <-stopCh:
klog.Info("etcd server stopped")
return nil
case err := <-etcd.Err():
klog.Error("error encountered in etcd server", err.Error())
return err

Check warning on line 89 in pkg/storage/etcd/new.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/etcd/new.go#L83-L89

Added lines #L83 - L89 were not covered by tests
}
}
5 changes: 5 additions & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ var (
ClusterEphemeralReportsGR = reportsv1.Resource("clusterephemeralreports")
PolicyReportsGR = v1alpha2.Resource("policyreports")
ClusterPolicyReportsGR = v1alpha2.Resource("clusterephemeralreports")

EphemeralReportsGVK = reportsv1.SchemeGroupVersion.WithKind("EphemeralReport")
ClusterEphemeralReportsGVK = reportsv1.SchemeGroupVersion.WithKind("ClusterEphemeralReport")
PolicyReportsGVK = v1alpha2.SchemeGroupVersion.WithKind("PolicyReport")
ClusterPolicyReportsGVK = v1alpha2.SchemeGroupVersion.WithKind("ClusterEphemeralReport")
)

0 comments on commit 632f09d

Please sign in to comment.