Skip to content

Commit

Permalink
Adding Ability to MergePatch CRDs properly (#488)
Browse files Browse the repository at this point in the history
* adding ability to merge patch crds properly

* added test for instance creation in an operator

* added specific error case for UnsupportedMediaType StrategicMergePatch

* added Kubernetes helper function and fixed linter

* linter fix
  • Loading branch information
fabianbaier authored Jul 9, 2019
1 parent 8de6e2e commit 8f5f345
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/controller/planexecution/planexecution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,33 @@ func (r *ReconcilePlanExecution) Reconcile(request reconcile.Request) (reconcile
log.Printf("Error getting patch between truth and obj: %v\n", err)
} else {
err = r.Client.Patch(context.TODO(), truth, client.ConstantPatch(types.StrategicMergePatchType, rawObj))
log.Printf("PlanExecutionController: CreateOrUpdate Patch: %v", err)
if err != nil {
// Right now applying a Strategic Merge Patch to custom resources does not work. There is
// certain metadata needed, which when missing, leads to an invalid Content-Type Header and
// causes the request to fail.
// ( see https://github.com/kubernetes-sigs/kustomize/issues/742#issuecomment-458650435 )
//
// We temporarily solve this by checking for the specific error when a SMP is applied to
// custom resources and handle it by defaulting to a Merge Patch.
//
// The error message for which we check is:
// the body of the request was in an unknown format - accepted media types include:
// application/json-patch+json, application/merge-patch+json
//
// Reason: "UnsupportedMediaType" Code: 415
if errors.IsUnsupportedMediaType(err) {
err = r.Client.Patch(context.TODO(), truth, client.ConstantPatch(types.MergePatchType, rawObj))
if err != nil {
log.Printf("PlanExecutionController: CreateOrUpdate MergePatch: %v", err)
}
} else {
log.Printf("PlanExecutionController: CreateOrUpdate StrategicMergePatch: Unknown Error: %v", err)
}
}
}
} else {
//create
log.Printf("PlanExecutionController: CreateOrUpdate Object not present")
log.Println("PlanExecutionController: CreateOrUpdate Object not present")
err = r.Client.Create(context.TODO(), obj)
log.Printf("PlanExecutionController: CreateOrUpdate Create: %v", err)
}
Expand Down
50 changes: 50 additions & 0 deletions test/integration/create-operator-in-operator/00-dream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Dream is the umbrella operator that tests the creation of instances within an operator
apiVersion: kudo.k8s.io/v1alpha1
kind: Operator
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: dream
---
apiVersion: kudo.k8s.io/v1alpha1
kind: OperatorVersion
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: dream-v1
spec:
version: "1.0.0"
connectionString: ""
operator:
name: dream
kind: Operator
templates:
operator.yaml: |
apiVersion: kudo.k8s.io/v1alpha1
kind: Instance
metadata:
name: operator
spec:
operatorVersion:
kind: OperatorVersion
name: test-operator-1.0
namespace: default
parameters:
REPLICAS: "0"
tasks:
operator:
resources:
- operator.yaml
parameters:
- name: Param
description: "Sample parameter"
default: "dream-in-a-dream"
plans:
deploy:
strategy: serial
phases:
- name: dependencies
steps:
- name: operator
tasks:
- operator
20 changes: 20 additions & 0 deletions test/integration/create-operator-in-operator/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: kudo.k8s.io/v1alpha1
kind: Instance
metadata:
name: dream1
status:
status: COMPLETE
---
apiVersion: kudo.k8s.io/v1alpha1
kind: Instance
metadata:
name: dream1-operator
status:
status: COMPLETE
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dream1-operator-nginx
spec:
replicas: 0
11 changes: 11 additions & 0 deletions test/integration/create-operator-in-operator/01-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: kudo.k8s.io/v1alpha1
kind: Instance
metadata:
labels:
controller-tools.k8s.io: "1.0"
operator: dream
name: dream1
spec:
operatorVersion:
name: dream-v1
kind: OperatorVersion

0 comments on commit 8f5f345

Please sign in to comment.