Skip to content

Commit

Permalink
CNF-14658: Replace clusterTemplateRef field with templateName and tem…
Browse files Browse the repository at this point in the history
…plateVersion (#221)

* Replace clusterTemplateRef field with templateName and templateVersion

* Fix unittest after rebase
  • Loading branch information
Missxiaoguo authored Oct 1, 2024
1 parent 6c9cc27 commit a052f08
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 47 deletions.
11 changes: 9 additions & 2 deletions api/v1alpha1/clusterrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ type ClusterRequestSpec struct {
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Location Spec",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
hwv1alpha1.LocationSpec `json:",inline"`

//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Cluster Template Reference",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
ClusterTemplateRef string `json:"clusterTemplateRef"`
// TemplateName defines the base name of the referenced ClusterTemplate.
// The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Template Name",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
TemplateName string `json:"templateName"`

// TemplateVersion defines the version of the referenced ClusterTemplate.
// The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Template Version",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
TemplateVersion string `json:"templateVersion"`

//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Cluster Template Input",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text"}
ClusterTemplateInput ClusterTemplateInput `json:"clusterTemplateInput"`
Expand Down
15 changes: 12 additions & 3 deletions bundle/manifests/o2ims.oran.openshift.io_clusterrequests.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions bundle/manifests/oran-o2ims.clusterserviceversion.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions config/crd/bases/o2ims.oran.openshift.io_clusterrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ spec:
- clusterInstanceInput
- policyTemplateInput
type: object
clusterTemplateRef:
type: string
location:
description: Location
type: string
site:
description: Site
type: string
templateName:
description: |-
TemplateName defines the base name of the referenced ClusterTemplate.
The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
type: string
templateVersion:
description: |-
TemplateVersion defines the version of the referenced ClusterTemplate.
The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
type: string
timeout:
description: |-
Timeout contains timeout values for hardware provisioning, cluster provisioning and
Expand All @@ -92,8 +100,9 @@ spec:
type: object
required:
- clusterTemplateInput
- clusterTemplateRef
- site
- templateName
- templateVersion
type: object
status:
description: ClusterRequestStatus defines the observed state of ClusterRequest
Expand Down
18 changes: 14 additions & 4 deletions config/manifests/bases/oran-o2ims.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ spec:
path: clusterTemplateInput
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- displayName: Cluster Template Reference
path: clusterTemplateRef
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- description: Location
displayName: Location
path: location
Expand All @@ -139,6 +135,20 @@ spec:
path: site
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- description: |-
TemplateName defines the base name of the referenced ClusterTemplate.
The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
displayName: Template Name
path: templateName
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- description: |-
TemplateVersion defines the version of the referenced ClusterTemplate.
The full name of the ClusterTemplate is constructed as <TemplateName.TemplateVersion>.
displayName: Template Version
path: templateVersion
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- displayName: Timeout
path: timeout
x-descriptors:
Expand Down
3 changes: 2 additions & 1 deletion config/samples/v1alpha1_clusterrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ spec:
clusterProvisioning: 80
configuration: 40
hardwareProvisioning: 60
clusterTemplateRef: clustertemplate-sample
templateName: clustertemplate-sample
templateVersion: v1.0.0
clusterTemplateInput:
policyTemplateInput:
sriov-network-vlan-1: "114"
Expand Down
2 changes: 1 addition & 1 deletion config/samples/v1alpha1_clustertemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
app.kubernetes.io/part-of: oran-o2ims
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: oran-o2ims
name: clustertemplate-sample
name: clustertemplate-sample.v1.0.0
spec:
templates:
clusterInstanceDefaults: clusterinstance-defaults-v1
Expand Down
24 changes: 18 additions & 6 deletions internal/controllers/clusterrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const (
ztpDoneLabel = "ztp-done"
)

func getClusterTemplateRefName(name, version string) string {
return fmt.Sprintf("%s.%s", name, version)
}

//+kubebuilder:rbac:groups=o2ims.oran.openshift.io,resources=clusterrequests,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=o2ims.oran.openshift.io,resources=clusterrequests/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=o2ims.oran.openshift.io,resources=clusterrequests/finalizers,verbs=update
Expand Down Expand Up @@ -1314,6 +1318,8 @@ func (t *clusterRequestReconcilerTask) createExtraManifestsConfigMap(
func (t *clusterRequestReconcilerTask) createPullSecret(
ctx context.Context, clusterInstance *siteconfig.ClusterInstance) error {

clusterTemplateRefName := getClusterTemplateRefName(
t.object.Spec.TemplateName, t.object.Spec.TemplateVersion)
// If we got to this point, we can assume that all the keys exist, including
// clusterName

Expand All @@ -1326,13 +1332,13 @@ func (t *clusterRequestReconcilerTask) createPullSecret(
if err != nil {
return fmt.Errorf(
"failed to check if pull secret %s exists in namespace %s: %w",
t.object.Spec.ClusterTemplateRef, t.object.Spec.ClusterTemplateRef, err,
pullSecretName, clusterTemplateRefName, err,
)
}
if !pullSecretExistsInTemplateNamespace {
return utils.NewInputError(
"pull secret %s expected to exist in the %s namespace, but it is missing",
pullSecretName, t.object.Spec.ClusterTemplateRef)
pullSecretName, clusterTemplateRefName)
}

newClusterInstancePullSecret := &corev1.Secret{
Expand Down Expand Up @@ -1565,8 +1571,10 @@ func (t *clusterRequestReconcilerTask) getCrClusterTemplateRef(ctx context.Conte
// Check the clusterTemplateRef references an existing template in the same namespace
// as the current clusterRequest.
clusterTemplateRef := &oranv1alpha1.ClusterTemplate{}
clusterTemplateRefName := getClusterTemplateRefName(
t.object.Spec.TemplateName, t.object.Spec.TemplateVersion)
clusterTemplateRefExists, err := utils.DoesK8SResourceExist(
ctx, t.client, t.object.Spec.ClusterTemplateRef, t.object.Namespace, clusterTemplateRef)
ctx, t.client, clusterTemplateRefName, t.object.Namespace, clusterTemplateRef)

// If there was an error in trying to get the ClusterTemplate, return it.
if err != nil {
Expand All @@ -1578,7 +1586,7 @@ func (t *clusterRequestReconcilerTask) getCrClusterTemplateRef(ctx context.Conte
return nil, utils.NewInputError(
fmt.Sprintf(
"the referenced ClusterTemplate (%s) does not exist in the %s namespace",
t.object.Spec.ClusterTemplateRef, t.object.Namespace))
clusterTemplateRefName, t.object.Namespace))
}
return clusterTemplateRef, nil
}
Expand All @@ -1598,11 +1606,13 @@ func (t *clusterRequestReconcilerTask) validateClusterTemplateInputMatchesSchema
}

// Check that the clusterTemplateInput matches the inputDataSchema from the ClusterTemplate.
clusterTemplateRefName := getClusterTemplateRefName(
t.object.Spec.TemplateName, t.object.Spec.TemplateVersion)
err = utils.ValidateJsonAgainstJsonSchema(
schemaMap, clusterTemplateInput)
if err != nil {
return fmt.Errorf("the provided clusterTemplateInput for %s does not "+
"match the schema from the ClusterTemplate (%s): %w", dataType, t.object.Spec.ClusterTemplateRef, err)
"match the schema from the ClusterTemplate (%s): %w", dataType, clusterTemplateRefName, err)
}

return nil
Expand Down Expand Up @@ -2134,7 +2144,9 @@ func (r *ClusterRequestReconciler) findClusterTemplateForClusterRequest(
// Create reconciling requests only for the clusterRequests that are using the
// current clusterTemplate.
for _, clusterRequest := range clusterRequests.Items {
if clusterRequest.Spec.ClusterTemplateRef == newClusterTemplate.GetName() {
clusterTemplateRefName := getClusterTemplateRefName(
clusterRequest.Spec.TemplateName, clusterRequest.Spec.TemplateVersion)
if clusterTemplateRefName == newClusterTemplate.GetName() {
r.Logger.Info(
"[findClusterRequestsForClusterTemplate] Add new reconcile request for ClusterRequest",
"name", clusterRequest.Name)
Expand Down
Loading

0 comments on commit a052f08

Please sign in to comment.