-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: start controllers only on supported platforms or unsupported co…
…ntroller instead
- Loading branch information
Showing
9 changed files
with
284 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package unsupported | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/klog/v2/klogr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
"github.com/openshift/cluster-capi-operator/pkg/controllers" | ||
"github.com/openshift/cluster-capi-operator/pkg/test" | ||
) | ||
|
||
var ( | ||
testEnv *envtest.Environment | ||
cfg *rest.Config | ||
cl client.Client | ||
) | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Controller Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
logf.SetLogger(klogr.New()) | ||
|
||
By("bootstrapping test environment") | ||
var err error | ||
testEnv = &envtest.Environment{} | ||
cfg, cl, err = test.StartEnvTest(testEnv) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cfg).NotTo(BeNil()) | ||
Expect(cl).NotTo(BeNil()) | ||
|
||
managedNamespace := &corev1.Namespace{} | ||
managedNamespace.SetName(controllers.DefaultManagedNamespace) | ||
Expect(cl.Create(context.Background(), managedNamespace)).To(Succeed()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
Expect(test.StopEnvTest(testEnv)).To(Succeed()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package unsupported | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
"github.com/openshift/cluster-capi-operator/pkg/controllers" | ||
"github.com/openshift/cluster-capi-operator/pkg/operatorstatus" | ||
) | ||
|
||
const ( | ||
capiUnsupportedPlatformMsg = "Cluster API is not yet implemented on this platform" | ||
) | ||
|
||
// UnsupportedController on unsupported platforms watches and keeps the cluster-api ClusterObject up to date. | ||
type UnsupportedController struct { | ||
operatorstatus.ClusterOperatorStatusClient | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
// Reconcile reconciles the cluster-api ClusterOperator object. | ||
func (r *UnsupportedController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := ctrl.LoggerFrom(ctx).WithName("UnsupportedController") | ||
log.Info(fmt.Sprintf("Reconciling %q ClusterObject", controllers.ClusterOperatorName)) | ||
|
||
if err := r.ClusterOperatorStatusClient.SetStatusAvailable(ctx, capiUnsupportedPlatformMsg); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to set conditions for %q ClusterObject: %w", controllers.ClusterOperatorName, err) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *UnsupportedController) SetupWithManager(mgr ctrl.Manager) error { | ||
build := ctrl.NewControllerManagedBy(mgr). | ||
For(&configv1.ClusterOperator{}, builder.WithPredicates(clusterOperatorPredicates())) | ||
|
||
return build.Complete(r) | ||
} |
81 changes: 81 additions & 0 deletions
81
pkg/controllers/unsupported/unsupported_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package unsupported | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
"github.com/openshift/cluster-capi-operator/pkg/operatorstatus" | ||
"github.com/openshift/cluster-capi-operator/pkg/test" | ||
) | ||
|
||
var _ = Describe("CAPI unsupported controller", func() { | ||
ctx := context.Background() | ||
var r *UnsupportedController | ||
var capiClusterOperator *configv1.ClusterOperator | ||
capiClusterOperatorKey := client.ObjectKey{Name: "cluster-api"} | ||
|
||
BeforeEach(func() { | ||
r = &UnsupportedController{ | ||
ClusterOperatorStatusClient: operatorstatus.ClusterOperatorStatusClient{ | ||
Client: cl, | ||
}, | ||
} | ||
|
||
capiClusterOperator = &configv1.ClusterOperator{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster-api", | ||
}, | ||
} | ||
|
||
Expect(cl.Create(ctx, capiClusterOperator)).To(Succeed(), "should be able to create the 'cluster-api' ClusterOperator object") | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(test.CleanupAndWait(ctx, cl, &configv1.ClusterOperator{})).To(Succeed()) | ||
}) | ||
|
||
It("should update cluster-api ClusterOperator status with an 'unsupported' message", func() { | ||
_, err := r.Reconcile(ctx, reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: capiClusterOperator.Name, | ||
}, | ||
}) | ||
|
||
Expect(err).ToNot(HaveOccurred(), "should be able to reconcile the cluster-api ClusterOperator without erroring") | ||
|
||
Eventually(func() (*configv1.ClusterOperator, error) { | ||
err := cl.Get(ctx, capiClusterOperatorKey, capiClusterOperator) | ||
return capiClusterOperator, err | ||
}).Should(HaveField("Status.Conditions", | ||
SatisfyAll( | ||
ContainElement(And(HaveField("Type", Equal(configv1.OperatorAvailable)), HaveField("Status", Equal(configv1.ConditionTrue)), HaveField("Message", Equal(capiUnsupportedPlatformMsg)))), | ||
ContainElement(And(HaveField("Type", Equal(configv1.OperatorProgressing)), HaveField("Status", Equal(configv1.ConditionFalse)))), | ||
ContainElement(And(HaveField("Type", Equal(configv1.OperatorDegraded)), HaveField("Status", Equal(configv1.ConditionFalse)))), | ||
), | ||
), "should match the expected ClusterOperator status conditions") | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package unsupported | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
"github.com/openshift/cluster-capi-operator/pkg/controllers" | ||
) | ||
|
||
// clusterOperatorPredicates defines a predicate function for the cluster-api ClusterOperator. | ||
func clusterOperatorPredicates() predicate.Funcs { | ||
isClusterOperator := func(obj runtime.Object) bool { | ||
clusterOperator, ok := obj.(*configv1.ClusterOperator) | ||
return ok && clusterOperator.GetName() == controllers.ClusterOperatorName | ||
} | ||
|
||
return predicate.Funcs{ | ||
CreateFunc: func(e event.CreateEvent) bool { return isClusterOperator(e.Object) }, | ||
UpdateFunc: func(e event.UpdateEvent) bool { return isClusterOperator(e.ObjectNew) }, | ||
GenericFunc: func(e event.GenericEvent) bool { return isClusterOperator(e.Object) }, | ||
DeleteFunc: func(e event.DeleteEvent) bool { return isClusterOperator(e.Object) }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters