Skip to content

Commit

Permalink
add resource apply and delete functions for validating webhook config…
Browse files Browse the repository at this point in the history
…urations

Signed-off-by: Vandit Singh <[email protected]>
  • Loading branch information
Vandit1604 committed Aug 9, 2024
1 parent 34a189d commit 8c798bd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ require (
google.golang.org/grpc v1.64.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.30.2
k8s.io/api v0.30.3
k8s.io/apiextensions-apiserver v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/apimachinery v0.30.3
k8s.io/client-go v0.30.2
k8s.io/cloud-provider-gcp v0.0.0-20231031161848-992c1c33f1be
sigs.k8s.io/aws-iam-authenticator v0.6.20
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,14 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.30.2 h1:+ZhRj+28QT4UOH+BKznu4CBgPWgkXO7XAvMcMl0qKvI=
k8s.io/api v0.30.2/go.mod h1:ULg5g9JvOev2dG0u2hig4Z7tQ2hHIuS+m8MNZ+X6EmI=
k8s.io/api v0.30.3 h1:ImHwK9DCsPA9uoU3rVh4QHAHHK5dTSv1nxJUapx8hoQ=
k8s.io/api v0.30.3/go.mod h1:GPc8jlzoe5JG3pb0KJCSLX5oAFIW3/qNJITlDj8BH04=
k8s.io/apiextensions-apiserver v0.30.2 h1:l7Eue2t6QiLHErfn2vwK4KgF4NeDgjQkCXtEbOocKIE=
k8s.io/apiextensions-apiserver v0.30.2/go.mod h1:lsJFLYyK40iguuinsb3nt+Sj6CmodSI4ACDLep1rgjw=
k8s.io/apimachinery v0.30.2 h1:fEMcnBj6qkzzPGSVsAZtQThU62SmQ4ZymlXRC5yFSCg=
k8s.io/apimachinery v0.30.2/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/apimachinery v0.30.3 h1:q1laaWCmrszyQuSQCfNB8cFgCuDAoPszKY4ucAjDwHc=
k8s.io/apimachinery v0.30.3/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.2 h1:sBIVJdojUNPDU/jObC+18tXWcTJVcwyqS9diGdWHk50=
k8s.io/client-go v0.30.2/go.mod h1:JglKSWULm9xlJLx4KCkfLLQ7XwtlbflV6uFFSHTMgVs=
k8s.io/cloud-provider-gcp v0.0.0-20231031161848-992c1c33f1be h1:GIzgPD2/+Ci+p8ePVgY03rZQwsIq6A4XLf+IUbTHCY0=
Expand Down
64 changes: 62 additions & 2 deletions pkg/provider/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/pkg/errors"
"gopkg.in/alecthomas/kingpin.v2"
admissionregistration "k8s.io/api/admissionregistration/v1"
appsV1 "k8s.io/api/apps/v1"
batchV1 "k8s.io/api/batch/v1"
apiCoreV1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -144,7 +145,6 @@ func (c *K8s) DeploymentsParse(*kingpin.ParseContext) error {
// ResourceApply applies k8s objects.
// The input is a slice of structs containing the filename and the slice of k8s objects present in the file.
func (c *K8s) ResourceApply(deployments []Resource) error {

var err error
for _, deployment := range deployments {
for _, resource := range deployment.Objects {
Expand Down Expand Up @@ -181,6 +181,8 @@ func (c *K8s) ResourceApply(deployments []Resource) error {
err = c.statefulSetApply(resource)
case "job":
err = c.jobApply(resource)
case "validatingwebhookconfiguration":
err = c.validatingWebhookConfigurationApply(resource)
default:
err = fmt.Errorf("creating request for unimplimented resource type:%v", kind)
}
Expand All @@ -195,7 +197,6 @@ func (c *K8s) ResourceApply(deployments []Resource) error {
// ResourceDelete deletes k8s objects.
// The input is a slice of structs containing the filename and the slice of k8s objects present in the file.
func (c *K8s) ResourceDelete(deployments []Resource) error {

var err error
for _, deployment := range deployments {
for _, resource := range deployment.Objects {
Expand Down Expand Up @@ -232,6 +233,8 @@ func (c *K8s) ResourceDelete(deployments []Resource) error {
err = c.statefulSetDelete(resource)
case "job":
err = c.jobDelete(resource)
case "validatingwebhookconfiguration":
err = c.ingressClassDelete(resource)
default:
err = fmt.Errorf("deleting request for unimplimented resource type:%v", kind)
}
Expand Down Expand Up @@ -546,6 +549,44 @@ func (c *K8s) jobApply(resource runtime.Object) error {
func() (bool, error) { return c.jobReady(resource) })
}

func (c *K8s) validatingWebhookConfigurationApply(resource runtime.Object) error {
req := resource.(*admissionregistration.ValidatingWebhookConfiguration)
kind := resource.GetObjectKind().GroupVersionKind().Kind

switch v := resource.GetObjectKind().GroupVersionKind().Version; v {
case "v1":
client := c.clt.AdmissionregistrationV1().ValidatingWebhookConfigurations()
list, err := client.List(c.ctx, apiMetaV1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "error listing resource : %v, name: %v", kind, req.Name)
}
var exists bool
for _, l := range list.Items {
if l.Name == req.Name {
exists = true
break
}
}

if exists {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
_, err := client.Update(c.ctx, req, apiMetaV1.UpdateOptions{})
return err
}); err != nil {
return errors.Wrapf(err, "resource update failed - kind: %v, name: %v", kind, req.Name)
}
log.Printf("resource updated - kind: %v, name: %v", kind, req.Name)
return nil
} else if _, err := client.Create(c.ctx, req, apiMetaV1.CreateOptions{}); err != nil {
return errors.Wrapf(err, "resource creation failed - kind: %v, name: %v", kind, req.Name)
}
log.Printf("resource created - kind: %v, name: %v", kind, req.Name)
default:
return fmt.Errorf("unknown object version: %v kind:'%v', name:'%v'", v, kind, req.Name)
}
return nil
}

func (c *K8s) customResourceApply(resource runtime.Object) error {
req := resource.(*apiServerExtensionsV1beta1.CustomResourceDefinition)
kind := resource.GetObjectKind().GroupVersionKind().Kind
Expand Down Expand Up @@ -967,6 +1008,7 @@ func (c *K8s) clusterRoleBindingDelete(resource runtime.Object) error {
}
return nil
}

func (c *K8s) configMapDelete(resource runtime.Object) error {
req := resource.(*apiCoreV1.ConfigMap)
kind := resource.GetObjectKind().GroupVersionKind().Kind
Expand Down Expand Up @@ -1260,6 +1302,24 @@ func (c *K8s) persistentVolumeClaimDelete(resource runtime.Object) error {
return nil
}

func (c *K8s) validatingWebhookConfigurationDelete(resource runtime.Object) error {
req := resource.(*admissionregistration.ValidatingWebhookConfiguration)
kind := resource.GetObjectKind().GroupVersionKind().Kind

switch v := resource.GetObjectKind().GroupVersionKind().Version; v {
case "v1":
client := c.clt.AdmissionregistrationV1().ValidatingWebhookConfigurations()
delPolicy := apiMetaV1.DeletePropagationForeground
if err := client.Delete(c.ctx, req.Name, apiMetaV1.DeleteOptions{PropagationPolicy: &delPolicy}); err != nil {
return errors.Wrapf(err, "resource delete failed - kind: %v, name: %v", kind, req.Name)
}
log.Printf("resource deleted - kind: %v , name: %v", kind, req.Name)
default:
return fmt.Errorf("unknown object version: %v kind:'%v', name:'%v'", v, kind, req.Name)
}
return nil
}

func (c *K8s) serviceExists(resource runtime.Object) (bool, error) {
req := resource.(*apiCoreV1.Service)
kind := resource.GetObjectKind().GroupVersionKind().Kind
Expand Down

0 comments on commit 8c798bd

Please sign in to comment.