Skip to content

Commit

Permalink
Abort plan with FATAL_ERROR if an unknown custom resource deployed (#…
Browse files Browse the repository at this point in the history
…1648)

* Abort plan with FATAL_ERROR if an unknown custom resource is going to be deployed.
* Add health check for CRDs so they can deployed correctly before next step

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
  • Loading branch information
ANeumann82 committed Aug 25, 2020
1 parent 6b10260 commit b8f7233
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 350 deletions.
10 changes: 10 additions & 0 deletions pkg/engine/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"reflect"

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -61,6 +62,15 @@ func IsHealthy(obj runtime.Object) error {

objUnstructured := &unstructured.Unstructured{Object: unstructMap}
switch obj := obj.(type) {
case *v1beta1.CustomResourceDefinition:
for _, c := range obj.Status.Conditions {
if c.Type == v1beta1.Established && c.Status == v1beta1.ConditionTrue {
log.Printf("CRD %s is now healthy", obj.Name)
return nil
}
}
msg := fmt.Sprintf("CRD %s is not healthy ( Conditions: %v )", obj.Name, obj.Status.Conditions)
return errors.New(msg)
case *appsv1.StatefulSet:
statusViewer := &polymorphichelpers.StatefulSetStatusViewer{}
msg, done, err := statusViewer.Status(objUnstructured, 0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/renderer/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (de *DefaultEnhancer) Apply(sourceObjs []runtime.Object, metadata Metadata)

isNamespaced, err := resource.IsNamespacedObject(obj, de.Discovery)
if err != nil {
return nil, fmt.Errorf("failed to determine if object %s is namespaced: %v", obj.GetObjectKind(), err)
return nil, fmt.Errorf("%wfailed to determine if object %s is namespaced: %v", engine.ErrFatalExecution, obj.GetObjectKind(), err)
}

// Note: Cross-namespace owner references are disallowed by design. This means:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apiVersion: kudo.dev/v1beta1
kind: Instance
metadata:
name: crd-instance
name: op-with-crd
spec:
operatorVersion:
name: crd-operator-0.1.0
name: op-with-crd-0.1.0
status:
planStatus:
deploy:
status: COMPLETE
status: FATAL_ERROR
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: kudo.dev/v1beta1
kind: TestStep
commands:
- command: kubectl kudo install --instance crd-instance ./operator
- command: kubectl kudo install --instance op-with-crd ./op-with-crd
namespaced: true
25 changes: 25 additions & 0 deletions test/integration/invalid-crd-install/op-with-crd/operator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: kudo.dev/v1beta1
name: "op-with-crd"
operatorVersion: "0.1.0"
appVersion: "1.7.9"
kubernetesVersion: 1.13.0
maintainers:
- name: Your name
email: <your@email.com>
url: https://kudo.dev
tasks:
- name: app
kind: Apply
spec:
resources:
- invalid-crd.yaml
plans:
deploy:
strategy: serial
phases:
- name: main
strategy: parallel
steps:
- name: everything
tasks:
- app
2 changes: 2 additions & 0 deletions test/integration/invalid-crd-install/op-with-crd/params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: kudo.dev/v1beta1
parameters:
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: somecrd.invalid/v1
kind: InvalidCrdType
metadata:
name: {{ .Name }}-resource
namespace: {{ .Namespace }}
spec:
this:
does-not: "matter"
there-is-no: "such-crd"
14 changes: 13 additions & 1 deletion test/integration/operator-with-custom-crd/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: servicemonitors.monitoring.coreos.com
name: servicemonitors.monitoring.coreos.com
---
apiVersion: kudo.dev/v1beta1
kind: Instance
metadata:
name: crd-instance
spec:
operatorVersion:
name: crd-operator-0.1.0
status:
planStatus:
deploy:
status: COMPLETE
Loading

0 comments on commit b8f7233

Please sign in to comment.