Skip to content

Commit

Permalink
Fixed lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Abhijit Mukherjee <abhijit.mukherjee@infracloud.io>
  • Loading branch information
mabhi committed Dec 19, 2023
1 parent 43b4c56 commit fde8d9f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
11 changes: 6 additions & 5 deletions cmd/reposervercontroller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import (
"github.com/kanisterio/kanister/pkg/resource"
"github.com/kanisterio/kanister/pkg/validatingwebhook"
//+kubebuilder:scaffold:imports
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

var (
Expand Down Expand Up @@ -80,7 +82,7 @@ func main() {
config := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Metrics: server.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
LeaderElection: false,
})
Expand Down Expand Up @@ -139,11 +141,10 @@ func main() {
// we can use validating webhook for k8s server versions < 1.25
if k8sserverVersion.Major == "1" && minorVersion < minorK8sVersion {
if validatingwebhook.IsCACertMounted() {
hookServer := mgr.GetWebhookServer()
webhook := admission.WithCustomValidator(&v1alpha1.RepositoryServer{}, &validatingwebhook.RepositoryServerValidator{})
hookServerOptions := webhook.Options{CertDir: validatingwebhook.WHCertsDir, Port: webhookServerPort}
hookServer := webhook.NewServer(hookServerOptions)
webhook := admission.WithCustomValidator(mgr.GetScheme(), &v1alpha1.RepositoryServer{}, &validatingwebhook.RepositoryServerValidator{})
hookServer.Register(whHandlePath, webhook)
hookServer.CertDir = validatingwebhook.WHCertsDir
hookServer.Port = webhookServerPort
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/blockstorage/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ var reVslmSyncFaultFatal = regexp.MustCompile("Change tracking invalid or disk i
func (p *FcdProvider) SnapshotCreate(ctx context.Context, volume blockstorage.Volume, tags map[string]string) (*blockstorage.Snapshot, error) {
var res types.AnyType
description := generateSnapshotDescription(tags)
err := wait.PollImmediate(time.Second, defaultRetryLimit, func() (bool, error) {

err := wait.PollUntilContextTimeout(ctx, time.Second, defaultRetryLimit, false, func(innerCtx context.Context) (bool, error) {
timeOfCreateSnapshotCall := time.Now()
var createErr error
res, createErr = p.createSnapshotAndWaitForCompletion(volume, ctx, description)
Expand Down Expand Up @@ -335,7 +336,7 @@ func (p *FcdProvider) SnapshotDelete(ctx context.Context, snapshot *blockstorage
if err != nil {
return errors.Wrap(err, "Cannot infer volume ID from full snapshot ID")
}
return wait.PollImmediate(time.Second, defaultRetryLimit, func() (bool, error) {
return wait.PollUntilContextTimeout(ctx, time.Second, defaultRetryLimit, false, func(innerCtx context.Context) (bool, error) {
log.Debug().Print("SnapshotDelete", field.M{"VolumeID": volID, "SnapshotID": snapshotID})
task, lerr := p.Gom.DeleteSnapshot(ctx, vimID(volID), vimID(snapshotID))
if lerr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
"github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
reposerver "github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
"github.com/kanisterio/kanister/pkg/testutil"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

const (
Expand Down Expand Up @@ -115,16 +117,16 @@ func (s *RepoServerControllerSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)

s.repoServerControllerNamespace = cns.Name

ws := webhook.NewServer(webhook.Options{Port: 9443})
// Since we are not creating the controller in a pod
// the repository server controller needs few env variables set explicitly
os.Setenv("POD_NAMESPACE", s.repoServerControllerNamespace)

mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
Port: 9443,
MetricsBindAddress: "0",
LeaderElection: false,
Scheme: scheme,
WebhookServer: ws,
Metrics: server.Options{BindAddress: "0"},
LeaderElection: false,
})
c.Assert(err, IsNil)

Expand Down
8 changes: 5 additions & 3 deletions pkg/customresource/customresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/client-go/util/retry"

// importing go check to bypass the testing flags
"golang.org/x/net/context"
_ "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -157,10 +158,11 @@ func rawCRDFromFile(path string) ([]byte, error) {
return yamls.ReadFile(path)
}

func waitForCRDInit(context Context, resource CustomResource) error {
func waitForCRDInit(crcontext Context, resource CustomResource) error {
crdName := fmt.Sprintf("%s.%s", resource.Plural, resource.Group)
return wait.Poll(context.Interval, context.Timeout, func() (bool, error) {
crd, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(contextpkg.TODO(), crdName, metav1.GetOptions{})
deadlineCtx, _ := context.WithTimeout(context.TODO(), crcontext.Timeout)
return wait.PollUntilContextTimeout(deadlineCtx, crcontext.Interval, crcontext.Timeout, false, func(ctx context.Context) (bool, error) {
crd, err := crcontext.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(contextpkg.TODO(), crdName, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ func (*healthCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// RunWebhookServer starts the validating webhook resources for blueprint kanister resources
func RunWebhookServer(c *rest.Config) error {
mgr, err := manager.New(c, manager.Options{})
if err != nil {
return errors.Wrapf(err, "Failed to create new webhook manager")
}
hookServerOptions := webhook.Options{CertDir: validatingwebhook.WHCertsDir}

hookServer := mgr.GetWebhookServer()
hookServer := webhook.NewServer(hookServerOptions)
hookServer.Register(whHandlePath, &webhook.Admission{Handler: &validatingwebhook.BlueprintValidator{}})
hookServer.Register(healthCheckPath, &healthCheckHandler{})
hookServer.Register(metricsPath, promhttp.Handler())

hookServer.CertDir = validatingwebhook.WHCertsDir
mgr, err := manager.New(c, manager.Options{
WebhookServer: hookServer,
})
if err != nil {
return errors.Wrapf(err, "Failed to create new webhook manager")
}

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions pkg/validatingwebhook/repositoryserver_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

type RepositoryServerValidator struct{}
Expand All @@ -31,25 +32,25 @@ var _ webhook.CustomValidator = &RepositoryServerValidator{}
//+kubebuilder:webhook:path=/validate/v1alpha1/repositoryserver,mutating=false,failurePolicy=fail,sideEffects=None,groups=cr.kanister.io,resources=repositoryservers,verbs=update,versions=v1alpha1,name=repositoryserver.cr.kanister.io,admissionReviewVersions=v1

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *RepositoryServerValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
return nil
func (r *RepositoryServerValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *RepositoryServerValidator) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) error {
func (r *RepositoryServerValidator) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (admission.Warnings, error) {
oldrs, ook := old.(*v1alpha1.RepositoryServer)
newrs, nok := new.(*v1alpha1.RepositoryServer)
if !ook || !nok {
return errors.New("Either updated object or the old object is not of type RepositoryServer.cr.kanister.io")
return nil, errors.New("Either updated object or the old object is not of type RepositoryServer.cr.kanister.io")
}
errMsg := fmt.Sprintf("RepositoryServer.cr.kanister.io \"%s\" is invalid: spec.repository.rootPath: Invalid value, Value is immutable", newrs.Name)
if oldrs.Spec.Repository.RootPath != newrs.Spec.Repository.RootPath {
return errors.New(errMsg)
return nil, errors.New(errMsg)
}
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *RepositoryServerValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
return nil
func (r *RepositoryServerValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return nil, nil
}

0 comments on commit fde8d9f

Please sign in to comment.