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 1a9ccda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 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
13 changes: 2 additions & 11 deletions pkg/service/service.go
Expand Up @@ -521,17 +521,8 @@ func PushKubernetesResource(client kclient.ClientInterface, u unstructured.Unstr
u.SetLabels(mergeLabels(existingLabels, map[string]string{"app.kubernetes.io/managed-by": "odo"}))
}

e := createOperatorService(client, u)
if e != nil {
if strings.Contains(e.Error(), "already exists") {
// this could be the case when "odo push" was executed after making change to code but there was no change to the service itself
// TODO: better way to handle this might be introduced by https://github.com/openshift/odo/issues/4553
return isOp, nil // this ensures that services slice is not updated
} else {
return isOp, e
}
}
return isOp, nil
err = createOperatorService(client, u)
return isOp, err
}

func isOperatorBackedService(client kclient.ClientInterface, u unstructured.Unstructured) (bool, error) {
Expand Down

0 comments on commit 1a9ccda

Please sign in to comment.