-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 c500c08
Showing
120 changed files
with
16,725 additions
and
390 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
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 | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.