From dfeaf4184551b48b9f6412a5dcd929ebf3f0a23e Mon Sep 17 00:00:00 2001 From: shivdudhani Date: Tue, 18 Jun 2019 13:52:12 -0700 Subject: [PATCH] support dryRun parameter in client api --- pkg/dclient/certificates.go | 8 ++++---- pkg/dclient/client.go | 34 +++++++++++++++++++++++++--------- pkg/violation/builder.go | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/pkg/dclient/certificates.go b/pkg/dclient/certificates.go index c72e0289e4..8cbf23113f 100644 --- a/pkg/dclient/certificates.go +++ b/pkg/dclient/certificates.go @@ -58,7 +58,7 @@ func (c *Client) submitAndApproveCertificateRequest(req *certificates.Certificat for _, csr := range csrList.Items { if csr.GetName() == req.ObjectMeta.Name { - err := c.DeleteResouce(CSRs, "", csr.GetName()) + err := c.DeleteResouce(CSRs, "", csr.GetName(), false) if err != nil { return nil, fmt.Errorf("Unable to delete existing certificate request: %v", err) } @@ -67,7 +67,7 @@ func (c *Client) submitAndApproveCertificateRequest(req *certificates.Certificat } } - unstrRes, err := c.CreateResource(CSRs, "", req) + unstrRes, err := c.CreateResource(CSRs, "", req, false) if err != nil { return nil, err } @@ -209,7 +209,7 @@ func (c *Client) WriteTlsPair(props tls.TlsCertificateProps, pemPair *tls.TlsPem Type: v1.SecretTypeTLS, } - _, err := c.CreateResource(Secrets, props.Namespace, secret) + _, err := c.CreateResource(Secrets, props.Namespace, secret, false) if err == nil { glog.Infof("Secret %s is created", name) } @@ -223,7 +223,7 @@ func (c *Client) WriteTlsPair(props tls.TlsCertificateProps, pemPair *tls.TlsPem secret.Data[v1.TLSCertKey] = pemPair.Certificate secret.Data[v1.TLSPrivateKeyKey] = pemPair.PrivateKey - _, err = c.UpdateResource(Secrets, props.Namespace, secret) + _, err = c.UpdateResource(Secrets, props.Namespace, secret, false) if err != nil { return err } diff --git a/pkg/dclient/client.go b/pkg/dclient/client.go index ee504644ed..f859759068 100644 --- a/pkg/dclient/client.go +++ b/pkg/dclient/client.go @@ -115,34 +115,50 @@ func (c *Client) ListResource(resource string, namespace string) (*unstructured. } // DeleteResouce deletes the specified resource -func (c *Client) DeleteResouce(resource string, namespace string, name string) error { - return c.getResourceInterface(resource, namespace).Delete(name, &meta.DeleteOptions{}) +func (c *Client) DeleteResouce(resource string, namespace string, name string, dryRun bool) error { + options := meta.DeleteOptions{} + if dryRun { + options = meta.DeleteOptions{DryRun: []string{meta.DryRunAll}} + } + return c.getResourceInterface(resource, namespace).Delete(name, &options) } // CreateResource creates object for the specified resource/namespace -func (c *Client) CreateResource(resource string, namespace string, obj interface{}) (*unstructured.Unstructured, error) { +func (c *Client) CreateResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) { + options := meta.CreateOptions{} + if dryRun { + options = meta.CreateOptions{DryRun: []string{meta.DryRunAll}} + } // convert typed to unstructured obj if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil { - return c.getResourceInterface(resource, namespace).Create(unstructuredObj, meta.CreateOptions{}) + return c.getResourceInterface(resource, namespace).Create(unstructuredObj, options) } return nil, fmt.Errorf("Unable to create resource ") } // UpdateResource updates object for the specified resource/namespace -func (c *Client) UpdateResource(resource string, namespace string, obj interface{}) (*unstructured.Unstructured, error) { +func (c *Client) UpdateResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) { + options := meta.UpdateOptions{} + if dryRun { + options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}} + } // convert typed to unstructured obj if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil { - return c.getResourceInterface(resource, namespace).Update(unstructuredObj, meta.UpdateOptions{}) + return c.getResourceInterface(resource, namespace).Update(unstructuredObj, options) } return nil, fmt.Errorf("Unable to update resource ") } // UpdateStatusResource updates the resource "status" subresource -func (c *Client) UpdateStatusResource(resource string, namespace string, obj interface{}) (*unstructured.Unstructured, error) { +func (c *Client) UpdateStatusResource(resource string, namespace string, obj interface{}, dryRun bool) (*unstructured.Unstructured, error) { + options := meta.UpdateOptions{} + if dryRun { + options = meta.UpdateOptions{DryRun: []string{meta.DryRunAll}} + } // convert typed to unstructured obj if unstructuredObj := convertToUnstructured(obj); unstructuredObj != nil { - return c.getResourceInterface(resource, namespace).UpdateStatus(unstructuredObj, meta.UpdateOptions{}) + return c.getResourceInterface(resource, namespace).UpdateStatus(unstructuredObj, options) } return nil, fmt.Errorf("Unable to update resource ") } @@ -204,7 +220,7 @@ func (c *Client) GenerateResource(generator types.Generation, namespace string) glog.Errorf("Can't create a resource %s: %v", generator.Name, err) return nil } - _, err = c.CreateResource(rGVR.Resource, namespace, resource) + _, err = c.CreateResource(rGVR.Resource, namespace, resource, false) if err != nil { return err } diff --git a/pkg/violation/builder.go b/pkg/violation/builder.go index 3c7b9f0049..0c64843500 100644 --- a/pkg/violation/builder.go +++ b/pkg/violation/builder.go @@ -82,7 +82,7 @@ func (b *builder) processViolation(info Info) error { modifiedPolicy.Status.Violations = modifiedViolations // Violations are part of the status sub resource, so we can use the Update Status api instead of updating the policy object - _, err = b.client.UpdateStatusResource("policies/status", namespace, modifiedPolicy) + _, err = b.client.UpdateStatusResource("policies/status", namespace, modifiedPolicy, false) if err != nil { return err }