Skip to content

Commit

Permalink
Merge pull request Mirantis#287 from eromanova/templates
Browse files Browse the repository at this point in the history
Split Templates in multiple types
  • Loading branch information
Kshatrix authored Sep 12, 2024
2 parents 562cf61 + fa1078e commit 13da58b
Show file tree
Hide file tree
Showing 127 changed files with 1,445 additions and 369 deletions.
32 changes: 18 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate CustomResourceDefinition objects.
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=templates/hmc/templates/crds
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=$(PROVIDER_TEMPLATES_DIR)/hmc/templates/crds

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: set-hmc-version
set-hmc-version: yq
$(YQ) eval '.version = "$(VERSION)"' -i templates/hmc/Chart.yaml
$(YQ) eval '.version = "$(VERSION)"' -i templates/hmc-templates/Chart.yaml
$(YQ) eval '.image.tag = "$(VERSION)"' -i templates/hmc/values.yaml
$(YQ) eval '.version = "$(VERSION)"' -i $(PROVIDER_TEMPLATES_DIR)/hmc/Chart.yaml
$(YQ) eval '.version = "$(VERSION)"' -i $(PROVIDER_TEMPLATES_DIR)/hmc-templates/Chart.yaml
$(YQ) eval '.image.tag = "$(VERSION)"' -i $(PROVIDER_TEMPLATES_DIR)/hmc/values.yaml

.PHONY: hmc-chart-release
hmc-chart-release: set-hmc-version templates-generate ## Generate hmc helm chart
Expand All @@ -74,7 +74,7 @@ hmc-chart-release: set-hmc-version templates-generate ## Generate hmc helm chart
hmc-dist-release: $(HELM) $(YQ)
@mkdir -p dist
@printf "apiVersion: v1\nkind: Namespace\nmetadata:\n name: $(NAMESPACE)\n" > dist/install.yaml
$(HELM) template -n $(NAMESPACE) hmc templates/hmc >> dist/install.yaml
$(HELM) template -n $(NAMESPACE) hmc $(PROVIDER_TEMPLATES_DIR)/hmc >> dist/install.yaml
$(YQ) eval -i '.metadata.namespace = "hmc-system"' dist/install.yaml
$(YQ) eval -i '.metadata.annotations."meta.helm.sh/release-name" = "hmc"' dist/install.yaml
$(YQ) eval -i '.metadata.annotations."meta.helm.sh/release-namespace" = "hmc-system"' dist/install.yaml
Expand Down Expand Up @@ -123,23 +123,27 @@ add-license: addlicense
##@ Build

TEMPLATES_DIR := templates
PROVIDER_TEMPLATES_DIR := $(TEMPLATES_DIR)/provider
CHARTS_PACKAGE_DIR ?= $(LOCALBIN)/charts
$(CHARTS_PACKAGE_DIR): | $(LOCALBIN)
rm -rf $(CHARTS_PACKAGE_DIR)
mkdir -p $(CHARTS_PACKAGE_DIR)

CHARTS = $(patsubst $(TEMPLATES_DIR)/%,%,$(wildcard $(TEMPLATES_DIR)/*))
TEMPLATE_FOLDERS = $(patsubst $(TEMPLATES_DIR)/%,%,$(wildcard $(TEMPLATES_DIR)/*))

.PHONY: helm-package
helm-package: helm
@make $(patsubst %,package-chart-%,$(CHARTS))
helm-package: $(CHARTS_PACKAGE_DIR) helm
@make $(patsubst %,package-%-templates,$(TEMPLATE_FOLDERS))

package-%-templates:
@make TEMPLATES_SUBDIR=$(TEMPLATES_DIR)/$* $(patsubst %,package-chart-%,$(shell ls $(TEMPLATES_DIR)/$*))

lint-chart-%:
$(HELM) dependency update $(TEMPLATES_DIR)/$*
$(HELM) lint --strict $(TEMPLATES_DIR)/$*
$(HELM) dependency update $(TEMPLATES_SUBDIR)/$*
$(HELM) lint --strict $(TEMPLATES_SUBDIR)/$*

package-chart-%: $(CHARTS_PACKAGE_DIR) lint-chart-%
$(HELM) package --destination $(CHARTS_PACKAGE_DIR) $(TEMPLATES_DIR)/$*
package-chart-%: lint-chart-%
$(HELM) package --destination $(CHARTS_PACKAGE_DIR) $(TEMPLATES_SUBDIR)/$*

LD_FLAGS?= -s -w
LD_FLAGS += -X github.com/Mirantis/hmc/internal/build.Version=$(VERSION)
Expand Down Expand Up @@ -232,7 +236,7 @@ registry-undeploy:

.PHONY: hmc-deploy
hmc-deploy: helm
$(HELM) upgrade --values $(HMC_VALUES) --reuse-values --install --create-namespace hmc templates/hmc -n $(NAMESPACE)
$(HELM) upgrade --values $(HMC_VALUES) --reuse-values --install --create-namespace hmc $(PROVIDER_TEMPLATES_DIR)/hmc -n $(NAMESPACE)

.PHONY: dev-deploy
dev-deploy: ## Deploy HMC helm chart to the K8s cluster specified in ~/.kube/config.
Expand Down Expand Up @@ -279,7 +283,7 @@ dev-push: docker-build helm-push

.PHONY: dev-templates
dev-templates: templates-generate
$(KUBECTL) -n $(NAMESPACE) apply -f templates/hmc-templates/files/templates
$(KUBECTL) -n $(NAMESPACE) apply -f $(PROVIDER_TEMPLATES_DIR)/hmc-templates/files/templates

.PHONY: dev-aws-creds
dev-aws-creds: yq
Expand Down
62 changes: 62 additions & 0 deletions api/v1alpha1/clustertemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=clustertmpl
// +kubebuilder:printcolumn:name="valid",type="boolean",JSONPath=".status.valid",description="Valid",priority=0
// +kubebuilder:printcolumn:name="validationError",type="string",JSONPath=".status.validationError",description="Validation Error",priority=1
// +kubebuilder:printcolumn:name="description",type="string",JSONPath=".status.description",description="Description",priority=1

// ClusterTemplate is the Schema for the cluster templates API
type ClusterTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterTemplateSpec `json:"spec,omitempty"`
Status ClusterTemplateStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ClusterTemplateList contains a list of ClusterTemplate
type ClusterTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ClusterTemplate `json:"items"`
}

// ClusterTemplateSpec defines the desired state of ClusterTemplate
type ClusterTemplateSpec struct {
TemplateSpecMixin `json:",inline"`
}

// ClusterTemplateStatus defines the observed state of ClusterTemplate
type ClusterTemplateStatus struct {
TemplateStatusMixin `json:",inline"`
}

func (t *ClusterTemplate) GetSpec() *TemplateSpecMixin {
return &t.Spec.TemplateSpecMixin
}

func (t *ClusterTemplate) GetStatus() *TemplateStatusMixin {
return &t.Status.TemplateStatusMixin
}
62 changes: 62 additions & 0 deletions api/v1alpha1/providertemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=providertmpl,scope=Cluster
// +kubebuilder:printcolumn:name="valid",type="boolean",JSONPath=".status.valid",description="Valid",priority=0
// +kubebuilder:printcolumn:name="validationError",type="string",JSONPath=".status.validationError",description="Validation Error",priority=1
// +kubebuilder:printcolumn:name="description",type="string",JSONPath=".status.description",description="Description",priority=1

// ProviderTemplate is the Schema for the provider templates API
type ProviderTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ProviderTemplateSpec `json:"spec,omitempty"`
Status ProviderTemplateStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ProviderTemplateList contains a list of ProviderTemplate
type ProviderTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ProviderTemplate `json:"items"`
}

// ProviderTemplateSpec defines the desired state of ProviderTemplate
type ProviderTemplateSpec struct {
TemplateSpecMixin `json:",inline"`
}

// ProviderTemplateStatus defines the observed state of ProviderTemplate
type ProviderTemplateStatus struct {
TemplateStatusMixin `json:",inline"`
}

func (t *ProviderTemplate) GetSpec() *TemplateSpecMixin {
return &t.Spec.TemplateSpecMixin
}

func (t *ProviderTemplate) GetStatus() *TemplateStatusMixin {
return &t.Status.TemplateStatusMixin
}
62 changes: 62 additions & 0 deletions api/v1alpha1/servicetemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=svctmpl
// +kubebuilder:printcolumn:name="valid",type="boolean",JSONPath=".status.valid",description="Valid",priority=0
// +kubebuilder:printcolumn:name="validationError",type="string",JSONPath=".status.validationError",description="Validation Error",priority=1
// +kubebuilder:printcolumn:name="description",type="string",JSONPath=".status.description",description="Description",priority=1

// ServiceTemplate is the Schema for the service templates API
type ServiceTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ServiceTemplateSpec `json:"spec,omitempty"`
Status ServiceTemplateStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ServiceTemplateList contains a list of ServiceTemplate
type ServiceTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ServiceTemplate `json:"items"`
}

// ServiceTemplateSpec defines the desired state of ServiceTemplate
type ServiceTemplateSpec struct {
TemplateSpecMixin `json:",inline"`
}

// ServiceTemplateStatus defines the observed state of ServiceTemplate
type ServiceTemplateStatus struct {
TemplateStatusMixin `json:",inline"`
}

func (t *ServiceTemplate) GetSpec() *TemplateSpecMixin {
return &t.Spec.TemplateSpecMixin
}

func (t *ServiceTemplate) GetStatus() *TemplateStatusMixin {
return &t.Status.TemplateStatusMixin
}
69 changes: 8 additions & 61 deletions api/v1alpha1/template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@
package v1alpha1

import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

helmcontrollerv2 "github.com/fluxcd/helm-controller/api/v2"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

const (
// ManagementKind is the string representation of a Management.
ManagementKind = "Management"
// TemplateKind is the string representation of a Template.
TemplateKind = "Template"

// ChartAnnotationType is an annotation containing the type of Template.
ChartAnnotationType = "hmc.mirantis.com/type"
// ChartAnnotationInfraProviders is an annotation containing the CAPI infrastructure providers associated with Template.
ChartAnnotationInfraProviders = "hmc.mirantis.com/infrastructure-providers"
// ChartAnnotationBootstrapProviders is an annotation containing the CAPI bootstrap providers associated with Template.
Expand All @@ -37,28 +28,11 @@ const (
ChartAnnotationControlPlaneProviders = "hmc.mirantis.com/control-plane-providers"
)

// TemplateType specifies the type of template packaged as a helm chart.
// Should be provided in the chart Annotations.
type TemplateType string

const (
// TemplateTypeDeployment is the type used for creating HMC ManagedCluster objects
TemplateTypeDeployment TemplateType = "deployment"
// TemplateTypeProvider is the type used for adding CAPI providers in the HMC Management object.
TemplateTypeProvider TemplateType = "provider"
// TemplateTypeCore is the type used for HMC and CAPI core components
TemplateTypeCore TemplateType = "core"
)

// TemplateSpec defines the desired state of Template
type TemplateSpec struct {
// TemplateSpecMixin is a Template configuration common for all Template types
type TemplateSpecMixin struct {
// Helm holds a reference to a Helm chart representing the HMC template
// +kubebuilder:validation:Required
Helm HelmSpec `json:"helm"`
// Type specifies the type of the provided template.
// Should be set if not present in the Helm chart metadata.
// +kubebuilder:validation:Enum=deployment;provider;core
Type TemplateType `json:"type,omitempty"`
// Providers represent required/exposed CAPI providers depending on the template type.
// Should be set if not present in the Helm chart metadata.
Providers Providers `json:"providers,omitempty"`
Expand All @@ -80,8 +54,8 @@ type HelmSpec struct {
ChartRef *helmcontrollerv2.CrossNamespaceSourceReference `json:"chartRef,omitempty"`
}

// TemplateStatus defines the observed state of Template
type TemplateStatus struct {
// TemplateStatusMixin defines the observed state of Template common for all Template types
type TemplateStatusMixin struct {
TemplateValidationStatus `json:",inline"`
// Description contains information about the template.
// +optional
Expand All @@ -94,9 +68,6 @@ type TemplateStatus struct {
// Helm chart representing the template.
// +optional
ChartRef *helmcontrollerv2.CrossNamespaceSourceReference `json:"chartRef,omitempty"`
// Type specifies the type of the provided template, as discovered from the Helm chart metadata.
// +kubebuilder:validation:Enum=deployment;provider;core
Type TemplateType `json:"type,omitempty"`
// Providers represent required/exposed CAPI providers depending on the template type.
Providers Providers `json:"providers,omitempty"`
// ObservedGeneration is the last observed generation.
Expand All @@ -112,32 +83,8 @@ type TemplateValidationStatus struct {
ValidationError string `json:"validationError,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:resource:shortName=hmc-tmpl;tmpl
// +kubebuilder:printcolumn:name="type",type="string",JSONPath=".status.type",description="Type",priority=0
// +kubebuilder:printcolumn:name="valid",type="boolean",JSONPath=".status.valid",description="Valid",priority=0
// +kubebuilder:printcolumn:name="validationError",type="string",JSONPath=".status.validationError",description="Validation Error",priority=1
// +kubebuilder:printcolumn:name="description",type="string",JSONPath=".status.description",description="Description",priority=1

// Template is the Schema for the templates API
type Template struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TemplateSpec `json:"spec,omitempty"`
Status TemplateStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TemplateList contains a list of Template
type TemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Template `json:"items"`
}

func init() {
SchemeBuilder.Register(&Template{}, &TemplateList{})
SchemeBuilder.Register(&ClusterTemplate{}, &ClusterTemplateList{})
SchemeBuilder.Register(&ServiceTemplate{}, &ServiceTemplateList{})
SchemeBuilder.Register(&ProviderTemplate{}, &ProviderTemplateList{})
}
Loading

0 comments on commit 13da58b

Please sign in to comment.