Skip to content

Commit

Permalink
Revert "Upgrade client-go to v0.27.2 and controller-runtime to v0.15.0 (
Browse files Browse the repository at this point in the history
stolostron#1093)" (stolostron#1104)

This reverts commit 4ad378a.
  • Loading branch information
dislbenn authored Aug 30, 2023
1 parent a903e24 commit 25fda6b
Show file tree
Hide file tree
Showing 5 changed files with 706 additions and 293 deletions.
134 changes: 67 additions & 67 deletions api/v1/multiclusterhub_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,56 +34,57 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var blockDeletionResources = []struct {
Name string
GVK schema.GroupVersionKind
ExceptionTotal int
Exceptions []string
}{
{
Name: "ManagedCluster",
GVK: schema.GroupVersionKind{
Group: "cluster.open-cluster-management.io",
Version: "v1",
Kind: "ManagedClusterList",
var (
blockDeletionResources = []struct {
Name string
GVK schema.GroupVersionKind
ExceptionTotal int
Exceptions []string
}{
{
Name: "ManagedCluster",
GVK: schema.GroupVersionKind{
Group: "cluster.open-cluster-management.io",
Version: "v1",
Kind: "ManagedClusterList",
},
ExceptionTotal: 1,
Exceptions: []string{"local-cluster"},
},
ExceptionTotal: 1,
Exceptions: []string{"local-cluster"},
},
{
Name: "MultiClusterObservability",
GVK: schema.GroupVersionKind{
Group: "observability.open-cluster-management.io",
Version: "v1beta2",
Kind: "MultiClusterObservabilityList",
{
Name: "MultiClusterObservability",
GVK: schema.GroupVersionKind{
Group: "observability.open-cluster-management.io",
Version: "v1beta2",
Kind: "MultiClusterObservabilityList",
},
ExceptionTotal: 0,
Exceptions: []string{},
},
ExceptionTotal: 0,
Exceptions: []string{},
},
{
Name: "DiscoveryConfig",
GVK: schema.GroupVersionKind{
Group: "discovery.open-cluster-management.io",
Version: "v1",
Kind: "DiscoveryConfigList",
{
Name: "DiscoveryConfig",
GVK: schema.GroupVersionKind{
Group: "discovery.open-cluster-management.io",
Version: "v1",
Kind: "DiscoveryConfigList",
},
ExceptionTotal: 0,
Exceptions: []string{},
},
ExceptionTotal: 0,
Exceptions: []string{},
},
{
Name: "AgentServiceConfig",
GVK: schema.GroupVersionKind{
Group: "agent-install.openshift.io",
Version: "v1beta1",
Kind: "AgentServiceConfigList",
{
Name: "AgentServiceConfig",
GVK: schema.GroupVersionKind{
Group: "agent-install.openshift.io",
Version: "v1beta1",
Kind: "AgentServiceConfigList",
},
ExceptionTotal: 0,
Exceptions: []string{},
},
ExceptionTotal: 0,
Exceptions: []string{},
},
}
}
)

var (
mchlog = log.Log.WithName("multiclusterhub-resource")
Expand All @@ -107,95 +108,94 @@ func (r *MultiClusterHub) Default() {
var _ webhook.Validator = &MultiClusterHub{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *MultiClusterHub) ValidateCreate() (admission.Warnings, error) {
func (r *MultiClusterHub) ValidateCreate() error {
mchlog.Info("validate create", "name", r.Name)
multiClusterHubList := &MultiClusterHubList{}
if err := Client.List(context.Background(), multiClusterHubList); err != nil {
return nil, fmt.Errorf("unable to list MultiClusterHubs: %s", err)
return fmt.Errorf("unable to list MultiClusterHubs: %s", err)
}

// Standalone MCH must exist before a hosted MCH can be created
if len(multiClusterHubList.Items) == 0 && r.IsInHostedMode() {
return nil, fmt.Errorf("a hosted Mode MCH can only be created once a non-hosted MCH is present")
return fmt.Errorf("a hosted Mode MCH can only be created once a non-hosted MCH is present")
}

// Prevent two standaline MCH's
for _, existing := range multiClusterHubList.Items {
existingMCH := existing
if !r.IsInHostedMode() && !existingMCH.IsInHostedMode() {
return nil, fmt.Errorf("MultiClusterHub in Standalone mode already exists: `%s`. "+
"Only one resource may exist in Standalone mode", existingMCH.Name)
return fmt.Errorf("MultiClusterHub in Standalone mode already exists: `%s`. Only one resource may exist in Standalone mode", existingMCH.Name)
}
}

if (r.Spec.AvailabilityConfig != HABasic) && (r.Spec.AvailabilityConfig != HAHigh) && (r.Spec.AvailabilityConfig != "") {
return nil, fmt.Errorf("invalid AvailabilityConfig given")
return fmt.Errorf("invalid AvailabilityConfig given")
}

// Validate components
if r.Spec.Overrides != nil {
for _, c := range r.Spec.Overrides.Components {
if !ValidComponent(c) {
return nil, fmt.Errorf("invalid component config: %s is not a known component", c.Name)
return fmt.Errorf("invalid component config: %s is not a known component", c.Name)
}
}
}

return nil, nil
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *MultiClusterHub) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
func (r *MultiClusterHub) ValidateUpdate(old runtime.Object) error {
mchlog.Info("validate update", "name", r.Name)

oldMCH := old.(*MultiClusterHub)

if oldMCH.Spec.SeparateCertificateManagement != r.Spec.SeparateCertificateManagement {
return nil, fmt.Errorf("updating SeparateCertificateManagement is forbidden")
return fmt.Errorf("updating SeparateCertificateManagement is forbidden")
}

if oldMCH.IsInHostedMode() != r.IsInHostedMode() {
return nil, fmt.Errorf("changes cannot be made to DeploymentMode")
return fmt.Errorf("changes cannot be made to DeploymentMode")
}

if !reflect.DeepEqual(oldMCH.Spec.Hive, r.Spec.Hive) {
return nil, fmt.Errorf("hive updates are forbidden")
return fmt.Errorf("hive updates are forbidden")
}

if (r.Spec.AvailabilityConfig != HABasic) && (r.Spec.AvailabilityConfig != HAHigh) && (r.Spec.AvailabilityConfig != "") {
return nil, fmt.Errorf("invalid AvailabilityConfig given")
return fmt.Errorf("invalid AvailabilityConfig given")
}

// Validate components
if r.Spec.Overrides != nil {
for _, c := range r.Spec.Overrides.Components {
if !ValidComponent(c) {
return nil, fmt.Errorf("invalid componentconfig: %s is not a known component", c.Name)
return fmt.Errorf("invalid componentconfig: %s is not a known component", c.Name)
}
}
}
return nil, nil
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *MultiClusterHub) ValidateDelete() (admission.Warnings, error) {
func (r *MultiClusterHub) ValidateDelete() error {
mchlog.Info("validate delete", "name", r.Name)

ctx := context.Background()

// Do not block delete of hosted mode, which does not spawn the resources
if r.IsInHostedMode() {
return nil, nil
return nil
}

cfg, err := config.GetConfig()
if err != nil {
return nil, err
return err
}

c, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return nil, err
return err
}

for _, resource := range blockDeletionResources {
Expand All @@ -205,23 +205,23 @@ func (r *MultiClusterHub) ValidateDelete() (admission.Warnings, error) {
if err == nil {
// List all resources
if err := Client.List(ctx, list); err != nil {
return nil, fmt.Errorf("unable to list %s: %s", resource.Name, err)
return fmt.Errorf("unable to list %s: %s", resource.Name, err)
}
// If there are any unexpected resources, deny deletion
if len(list.Items) > resource.ExceptionTotal {
return nil, fmt.Errorf("cannot delete MultiClusterHub resource because %s resource(s) exist", resource.Name)
return fmt.Errorf("cannot delete MultiClusterHub resource because %s resource(s) exist", resource.Name)
}
// if exception resources are present, check if they are the same as the exception resources
if resource.ExceptionTotal > 0 {
for _, item := range list.Items {
if !contains(resource.Exceptions, item.GetName()) {
return nil, fmt.Errorf("cannot delete MultiClusterHub resource because %s resource(s) exist", resource.Name)
return fmt.Errorf("cannot delete MultiClusterHub resource because %s resource(s) exist", resource.Name)
}
}
}
}
}
return nil, nil
return nil
}

// ValidatingWebhook returns the ValidatingWebhookConfiguration used for the multiclusterhub
Expand Down
36 changes: 23 additions & 13 deletions controllers/multiclusterhub_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -88,7 +89,9 @@ const (
mceUpgradeDuration = 10 * time.Minute
)

var mceUpgradeStartTime = time.Time{}
var (
mceUpgradeStartTime = time.Time{}
)

//+kubebuilder:rbac:groups="";"admissionregistration.k8s.io";"apiextensions.k8s.io";"apiregistration.k8s.io";"apps";"apps.open-cluster-management.io";"authorization.k8s.io";"hive.openshift.io";"mcm.ibm.com";"proxy.open-cluster-management.io";"rbac.authorization.k8s.io";"security.openshift.io";"clusterview.open-cluster-management.io";"discovery.open-cluster-management.io";"wgpolicyk8s.io",resources=apiservices;channels;clusterjoinrequests;clusterrolebindings;clusterstatuses/log;configmaps;customresourcedefinitions;deployments;discoveryconfigs;hiveconfigs;mutatingwebhookconfigurations;validatingwebhookconfigurations;namespaces;pods;policyreports;replicasets;rolebindings;secrets;serviceaccounts;services;subjectaccessreviews;subscriptions;helmreleases;managedclusters;managedclustersets,verbs=get
//+kubebuilder:rbac:groups="";"admissionregistration.k8s.io";"apiextensions.k8s.io";"apiregistration.k8s.io";"apps";"apps.open-cluster-management.io";"authorization.k8s.io";"hive.openshift.io";"monitoring.coreos.com";"rbac.authorization.k8s.io";"mcm.ibm.com";"security.openshift.io",resources=apiservices;channels;clusterjoinrequests;clusterrolebindings;clusterroles;configmaps;customresourcedefinitions;deployments;hiveconfigs;mutatingwebhookconfigurations;validatingwebhookconfigurations;namespaces;rolebindings;secrets;serviceaccounts;services;servicemonitors;subjectaccessreviews;subscriptions;validatingwebhookconfigurations,verbs=create;update
Expand Down Expand Up @@ -531,8 +534,11 @@ func (r *MultiClusterHubReconciler) SetupWithManager(mgr ctrl.Manager) (controll
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
).
Watches(
&appsv1.Deployment{},
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &operatorv1.MultiClusterHub{}),
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &operatorv1.MultiClusterHub{},
},
builder.WithPredicates(
ctrlpredicate.Or(
ctrlpredicate.GenerationChangedPredicate{},
Expand All @@ -542,9 +548,9 @@ func (r *MultiClusterHubReconciler) SetupWithManager(mgr ctrl.Manager) (controll
),
).
Watches(
&apiregistrationv1.APIService{},
&source.Kind{Type: &apiregistrationv1.APIService{}},
handler.Funcs{
DeleteFunc: func(ctx context.Context, e event.DeleteEvent, q workqueue.RateLimitingInterface) {
DeleteFunc: func(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
labels := e.Object.GetLabels()
q.Add(
reconcile.Request{
Expand All @@ -558,9 +564,9 @@ func (r *MultiClusterHubReconciler) SetupWithManager(mgr ctrl.Manager) (controll
},
builder.WithPredicates(predicate.DeletePredicate{}),
).
Watches(&appsv1.Deployment{},
Watches(&source.Kind{Type: &appsv1.Deployment{}},
handler.EnqueueRequestsFromMapFunc(
func(ctx context.Context, a client.Object) []reconcile.Request {
func(a client.Object) []reconcile.Request {
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Expand All @@ -583,9 +589,9 @@ func (r *MultiClusterHubReconciler) SetupWithManager(mgr ctrl.Manager) (controll
),
).
Watches(
&configv1.ClusterVersion{},
&source.Kind{Type: &configv1.ClusterVersion{}},
handler.EnqueueRequestsFromMapFunc(
func(ctx context.Context, a client.Object) []reconcile.Request {
func(a client.Object) []reconcile.Request {
multiClusterHubList := &operatorv1.MultiClusterHubList{}
if err := r.Client.List(context.TODO(), multiClusterHubList); err == nil && len(multiClusterHubList.Items) > 0 {
mch := multiClusterHubList.Items[0]
Expand All @@ -610,6 +616,7 @@ func (r *MultiClusterHubReconciler) applyTemplate(ctx context.Context, m *operat
// Set owner reference.
if (template.GetKind() == "ClusterRole") || (template.GetKind() == "ClusterRoleBinding") || (template.GetKind() == "ServiceMonitor") || (template.GetKind() == "CustomResourceDefinition") {
utils.AddInstallerLabel(template, m.Name, m.Namespace)

}

if template.GetKind() == "APIService" {
Expand Down Expand Up @@ -671,8 +678,8 @@ func (r *MultiClusterHubReconciler) fetchChartLocation(ctx context.Context, comp
}

func (r *MultiClusterHubReconciler) ensureComponent(ctx context.Context, m *operatorv1.MultiClusterHub, component string,
images map[string]string,
) (ctrl.Result, error) {
images map[string]string) (ctrl.Result, error) {

log := log.FromContext(ctx)
chartLocation := r.fetchChartLocation(ctx, component)

Expand Down Expand Up @@ -706,8 +713,8 @@ func (r *MultiClusterHubReconciler) ensureComponent(ctx context.Context, m *oper
}

func (r *MultiClusterHubReconciler) ensureNoComponent(ctx context.Context, m *operatorv1.MultiClusterHub,
component string, images map[string]string,
) (result ctrl.Result, err error) {
component string, images map[string]string) (result ctrl.Result, err error) {

log := log.FromContext(ctx)
chartLocation := r.fetchChartLocation(ctx, component)

Expand Down Expand Up @@ -757,6 +764,7 @@ func (r *MultiClusterHubReconciler) ensureNoComponent(ctx context.Context, m *op
}

func (r *MultiClusterHubReconciler) ensureClusterPermission(ctx context.Context, m *operatorv1.MultiClusterHub, images map[string]string) (ctrl.Result, error) {

log := log.FromContext(ctx)

// Render temmplates from file location
Expand Down Expand Up @@ -1113,6 +1121,7 @@ func updatePausedCondition(m *operatorv1.MultiClusterHub) {
condition := NewHubCondition(operatorv1.Progressing, metav1.ConditionTrue, ResumedReason, "Multiclusterhub is resumed")
SetHubCondition(&m.Status, *condition)
}

}
}

Expand Down Expand Up @@ -1202,4 +1211,5 @@ func (r *MultiClusterHubReconciler) setDefaults(m *operatorv1.MultiClusterHub, o
}
log.Info("No updates to defaults detected")
return ctrl.Result{}, nil

}
Loading

0 comments on commit 25fda6b

Please sign in to comment.