Skip to content

Commit

Permalink
Use server-side apply to apply Kubernetes components
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Nov 23, 2021
1 parent da5939f commit d7d8b00
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/kclient/deployments.go
Expand Up @@ -326,16 +326,17 @@ func (c *Client) DeleteDeployment(labels map[string]string) error {
}

// CreateDynamicResource creates a dynamic custom resource
func (c *Client) CreateDynamicResource(exampleCustomResource unstructured.Unstructured, gvr *meta.RESTMapping) error {
deployment := &unstructured.Unstructured{
Object: exampleCustomResource.Object,
func (c *Client) CreateDynamicResource(resource unstructured.Unstructured, gvr *meta.RESTMapping) error {
klog.V(5).Infoln("Applying resource via server-side apply:")
klog.V(5).Infoln(resourceAsJson(resource.Object))

data, err := json.Marshal(resource.Object)
if err != nil {
return errors.Wrapf(err, "unable to marshal resource")
}

debugOut, _ := json.MarshalIndent(deployment, " ", " ")
klog.V(5).Infoln("Creating resource:")
klog.V(5).Infoln(string(debugOut))
// Create the dynamic resource based on the alm-example for the CRD
_, err := c.DynamicClient.Resource(gvr.Resource).Namespace(c.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{FieldManager: FieldManager})
// Patch the dynamic resource
_, err = c.DynamicClient.Resource(gvr.Resource).Namespace(c.Namespace).Patch(context.TODO(), resource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{FieldManager: FieldManager, Force: boolPtr(true)})
if err != nil {
return err
}
Expand Down

0 comments on commit d7d8b00

Please sign in to comment.