Skip to content

Commit

Permalink
Move apis to v1beta1
Browse files Browse the repository at this point in the history
Introduce conversion based on webhook for CRDs having a clusterSelector
field:

- ClusterSet
- Set
- ClusterHealthCheck
- RoleRequest
  • Loading branch information
mgianluc committed Jun 17, 2024
1 parent d87beab commit f025e3e
Show file tree
Hide file tree
Showing 120 changed files with 16,700 additions and 365 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ help: ## Display this help.
##@ Development

.PHONY: manifests
manifests: $(CONTROLLER_GEN) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./..." output:crd:artifacts:config=config/crd/bases
$(KUSTOMIZE) build config/default -o manifests

## Generate go code for library.
crds: generate ## Generates go code (crds) for library
Expand Down
121 changes: 121 additions & 0 deletions api/v1alpha1/clusterhealthcheck_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.
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 v1alpha1

import (
"encoding/json"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/conversion"

libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
)

// ConvertTo converts v1alpha1 to the Hub version (v1beta1).
func (src *ClusterHealthCheck) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*libsveltosv1beta1.ClusterHealthCheck)

configlog.V(logs.LogInfo).Info("convert ClusterHealthCheck from v1alpha1 to v1beta1")

dst.ObjectMeta = src.ObjectMeta

jsonData, err := json.Marshal(src.Spec.LivenessChecks) // Marshal the LivenessChecks field
if err != nil {
return fmt.Errorf("error marshaling Spec.LivenessChecks: %w", err)
}
err = json.Unmarshal(jsonData, &dst.Spec.LivenessChecks) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

jsonData, err = json.Marshal(src.Spec.Notifications) // Marshal the Notifications field
if err != nil {
return fmt.Errorf("error marshaling Spec.Notifications: %w", err)
}
err = json.Unmarshal(jsonData, &dst.Spec.Notifications) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

selector, err := convertV1Alpha1SelectorToV1Beta1(&src.Spec.ClusterSelector)
if err != nil {
configlog.V(logs.LogInfo).Info(fmt.Sprintf("failed to convert ClusterSelector: %v", err))
return err
}

dst.Spec.ClusterSelector = *selector

jsonData, err = json.Marshal(src.Status) // Marshal the Status field
if err != nil {
return fmt.Errorf("error marshaling Status: %w", err)
}

err = json.Unmarshal(jsonData, &dst.Status) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this v1alpha1.
func (dst *ClusterHealthCheck) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*libsveltosv1beta1.ClusterHealthCheck)

configlog.V(logs.LogInfo).Info("convert ClusterHealthCheck from v1beta1 to v1alpha1")

dst.ObjectMeta = src.ObjectMeta

jsonData, err := json.Marshal(src.Spec.LivenessChecks) // Marshal the LivenessChecks field
if err != nil {
return fmt.Errorf("error marshaling Spec.LivenessChecks: %w", err)
}
err = json.Unmarshal(jsonData, &dst.Spec.LivenessChecks) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

jsonData, err = json.Marshal(src.Spec.Notifications) // Marshal the Notifications field
if err != nil {
return fmt.Errorf("error marshaling Spec.Notifications: %w", err)
}
err = json.Unmarshal(jsonData, &dst.Spec.Notifications) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

selector, err := convertV1Beta1SelectorToV1Alpha1(&src.Spec.ClusterSelector)
if err != nil {
configlog.V(logs.LogInfo).Info(fmt.Sprintf("failed to convert ClusterSelector: %v", err))
return err
}

dst.Spec.ClusterSelector = selector

jsonData, err = json.Marshal(src.Status) // Marshal the Status field
if err != nil {
return fmt.Errorf("error marshaling Status: %w", err)
}

err = json.Unmarshal(jsonData, &dst.Status) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

return nil
}
4 changes: 0 additions & 4 deletions api/v1alpha1/clusterhealthcheck_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ type ClusterHealthCheckSpec struct {
// ClusterSelector identifies clusters to associate to (Deprecated use selector instead).
ClusterSelector Selector `json:"clusterSelector"`

// Selector identifies clusters to associate to.
// +optional
Selector ClusterSelector `json:"selector,omitempty"`

// LivenessChecks is a list of source of liveness checks to evaluate.
// Anytime one of those changes, notifications will be sent
// +patchMergeKey=name
Expand Down
79 changes: 79 additions & 0 deletions api/v1alpha1/clusterset_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.
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 v1alpha1

import (
"encoding/json"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/conversion"

libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
)

// ConvertTo converts v1alpha1 to the Hub version (v1beta1).
func (src *ClusterSet) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*libsveltosv1beta1.ClusterSet)

configlog.V(logs.LogInfo).Info("convert ClusterSet from v1alpha1 to v1beta1")

dst.ObjectMeta = src.ObjectMeta

err := convertV1Alpha1SetSpecToV1Beta1(&src.Spec, &dst.Spec)
if err != nil {
return fmt.Errorf("error converting Spec: %w", err)
}

jsonData, err := json.Marshal(src.Status) // Marshal the Status field
if err != nil {
return fmt.Errorf("error marshaling Status: %w", err)
}

err = json.Unmarshal(jsonData, &dst.Status) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this v1alpha1.
func (dst *ClusterSet) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*libsveltosv1beta1.ClusterSet)

configlog.V(logs.LogInfo).Info("convert ClusterSet from v1beta1 to v1alpha1")

dst.ObjectMeta = src.ObjectMeta

err := convertV1Beta1SetSpecToV1Alpha1(&src.Spec, &dst.Spec)
if err != nil {
return fmt.Errorf("error converting Spec: %w", err)
}

jsonData, err := json.Marshal(src.Status) // Marshal the Status field
if err != nil {
return fmt.Errorf("error marshaling Status: %w", err)
}

err = json.Unmarshal(jsonData, &dst.Status) // Unmarshal to v1beta1 type
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

return nil
}
11 changes: 0 additions & 11 deletions api/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"errors"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

const (
Expand Down Expand Up @@ -73,15 +71,6 @@ var (

type Selector string

type ClusterSelector struct {
metav1.LabelSelector `json:",inline"`
}

// ToSelector converts ClusterSelector to labels.Selector
func (cs *ClusterSelector) ToSelector() (labels.Selector, error) {
return metav1.LabelSelectorAsSelector(&cs.LabelSelector)
}

// +kubebuilder:validation:Enum:=Provisioning;Provisioned;Failed;Removing;Removed
type SveltosFeatureStatus string

Expand Down
Loading

0 comments on commit f025e3e

Please sign in to comment.