Skip to content

Commit

Permalink
Merge pull request #26 from Kshatrix/templates-annotations
Browse files Browse the repository at this point in the history
Add chart annotations for existing templates
  • Loading branch information
Kshatrix authored Jun 13, 2024
2 parents 1bbe3f2 + 903e48b commit d7ab7da
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 deletions.
31 changes: 17 additions & 14 deletions api/v1alpha1/template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ const (

// ChartAnnotationType is an annotation containing the type of Template.
ChartAnnotationType = "hmc.mirantis.com/type"
// ChartAnnotationInfraProvider is an annotation containing the CAPI provider associated with Template.
ChartAnnotationInfraProvider = "hmc.mirantis.com/infrastructure-provider"
// ChartAnnotationBootstrapProvider is an annotation containing the k8s distribution associated with Template.
ChartAnnotationBootstrapProvider = "hmc.mirantis.com/bootstrap-provider"
// 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.
ChartAnnotationBootstrapProviders = "hmc.mirantis.com/bootstrap-providers"
// ChartAnnotationControlPlaneProviders is an annotation containing the CAPI control plane providers associated with Template.
ChartAnnotationControlPlaneProviders = "hmc.mirantis.com/control-plane-providers"
)

// TemplateType specifies the type of template packaged as a helm chart.
Expand All @@ -44,10 +46,8 @@ type TemplateType string
const (
// TemplateTypeDeployment is the type used for creating HMC Deployment objects
TemplateTypeDeployment TemplateType = "deployment"
// TemplateTypeInfraProvider is the type used for adding CAPI infrastructure providers in the HMC Management object
TemplateTypeInfraProvider TemplateType = "infrastructure-provider"
// TemplateTypeBootstrapProvider is the type used for adding CAPI bootstrap providers in the HMC Management object
TemplateTypeBootstrapProvider TemplateType = "bootstrap-provider"
// TemplateTypeProvider is the type used for adding CAPI providers in the HMC Management object.
TemplateTypeProvider TemplateType = "provider"
// TemplateTypeManagement is the type used for HMC management components
TemplateTypeManagement TemplateType = "management"
)
Expand Down Expand Up @@ -78,7 +78,7 @@ type HelmSpec struct {
// TemplateStatus defines the observed state of Template
type TemplateStatus struct {
TemplateValidationStatus `json:",inline"`
// Descriptions contains information about the template.
// Description contains information about the template.
// +optional
Description string `json:"description,omitempty"`
// Config demonstrates available parameters for template customization,
Expand All @@ -90,14 +90,17 @@ type TemplateStatus struct {
// +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;infrastructure-provider;bootstrap-provider;management
// +kubebuilder:validation:Enum=deployment;provider;management
Type string `json:"type,omitempty"`
// InfrastructureProvider specifies a CAPI infrastructure provider associated with the template.
// InfrastructureProviders specifies CAPI infrastructure providers associated with the template.
// +optional
InfrastructureProvider string `json:"infrastructureProvider,omitempty"`
// BootstrapProvider specifies a CAPI bootstrap provider associated with the template.
InfrastructureProviders []string `json:"infrastructureProviders,omitempty"`
// BootstrapProviders specifies CAPI bootstrap providers associated with the template.
// +optional
BootstrapProvider string `json:"bootstrapProvider,omitempty"`
BootstrapProviders []string `json:"bootstrapProviders,omitempty"`
// ControlPlaneProviders specifies CAPI control plane providers associated with the template.
// +optional
ControlPlaneProviders []string `json:"controlPlaneProviders,omitempty"`
// ObservedGeneration is the last observed generation.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Expand Down
15 changes: 15 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 2 additions & 0 deletions charts/hmc/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ version: 0.1.0
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
annotations:
hmc.mirantis.com/type: management
29 changes: 19 additions & 10 deletions charts/hmc/templates/template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ spec:
status:
description: TemplateStatus defines the observed state of Template
properties:
bootstrapProvider:
description: BootstrapProvider specifies a CAPI bootstrap provider associated
bootstrapProviders:
description: BootstrapProviders specifies CAPI bootstrap providers associated
with the template.
type: string
items:
type: string
type: array
chartRef:
description: |-
ChartRef is a reference to a source controller resource containing the
Expand Down Expand Up @@ -132,13 +134,21 @@ spec:
Config demonstrates available parameters for template customization,
that can be used when creating Deployment objects.
x-kubernetes-preserve-unknown-fields: true
controlPlaneProviders:
description: ControlPlaneProviders specifies CAPI control plane providers
associated with the template.
items:
type: string
type: array
description:
description: Descriptions contains information about the template.
type: string
infrastructureProvider:
description: InfrastructureProvider specifies a CAPI infrastructure
provider associated with the template.
description: Description contains information about the template.
type: string
infrastructureProviders:
description: InfrastructureProviders specifies CAPI infrastructure providers
associated with the template.
items:
type: string
type: array
observedGeneration:
description: ObservedGeneration is the last observed generation.
format: int64
Expand All @@ -148,8 +158,7 @@ spec:
from the Helm chart metadata.
enum:
- deployment
- infrastructure-provider
- bootstrap-provider
- provider
- management
type: string
valid:
Expand Down
29 changes: 19 additions & 10 deletions config/crd/bases/hmc.mirantis.com_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ spec:
status:
description: TemplateStatus defines the observed state of Template
properties:
bootstrapProvider:
description: BootstrapProvider specifies a CAPI bootstrap provider
bootstrapProviders:
description: BootstrapProviders specifies CAPI bootstrap providers
associated with the template.
type: string
items:
type: string
type: array
chartRef:
description: |-
ChartRef is a reference to a source controller resource containing the
Expand Down Expand Up @@ -131,13 +133,21 @@ spec:
Config demonstrates available parameters for template customization,
that can be used when creating Deployment objects.
x-kubernetes-preserve-unknown-fields: true
controlPlaneProviders:
description: ControlPlaneProviders specifies CAPI control plane providers
associated with the template.
items:
type: string
type: array
description:
description: Descriptions contains information about the template.
type: string
infrastructureProvider:
description: InfrastructureProvider specifies a CAPI infrastructure
provider associated with the template.
description: Description contains information about the template.
type: string
infrastructureProviders:
description: InfrastructureProviders specifies CAPI infrastructure
providers associated with the template.
items:
type: string
type: array
observedGeneration:
description: ObservedGeneration is the last observed generation.
format: int64
Expand All @@ -147,8 +157,7 @@ spec:
discovered from the Helm chart metadata.
enum:
- deployment
- infrastructure-provider
- bootstrap-provider
- provider
- management
type: string
valid:
Expand Down
42 changes: 17 additions & 25 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

v2 "github.com/fluxcd/helm-controller/api/v2"
Expand All @@ -45,10 +46,8 @@ const (
)

var (
errNoInfraProvider = fmt.Errorf("no infra provider specified: %s chart annotation must not be empty", hmc.ChartAnnotationInfraProvider)
errNoBootstrapProvider = fmt.Errorf("no bootstrap provider specified: %s chart annotation must not be empty", hmc.ChartAnnotationBootstrapProvider)
errNoProviderType = fmt.Errorf("template type is not supported: %s chart annotation must be one of [%s/%s/%s]",
hmc.ChartAnnotationType, hmc.TemplateTypeDeployment, hmc.ChartAnnotationInfraProvider, hmc.ChartAnnotationBootstrapProvider)
errNoProviderType = fmt.Errorf("template type is not supported: %s chart annotation must be one of [%s/%s]",
hmc.ChartAnnotationType, hmc.TemplateTypeDeployment, hmc.TemplateTypeProvider)
)

// TemplateReconciler reconciles a Template object
Expand Down Expand Up @@ -152,32 +151,25 @@ func (r *TemplateReconciler) parseChartMetadata(template *hmc.Template, chart *c
return fmt.Errorf("chart metadata is empty")
}
templateType := chart.Metadata.Annotations[hmc.ChartAnnotationType]
infraProvider := chart.Metadata.Annotations[hmc.ChartAnnotationInfraProvider]
bootstrapProvider := chart.Metadata.Annotations[hmc.ChartAnnotationBootstrapProvider]

switch hmc.TemplateType(templateType) {
case hmc.TemplateTypeDeployment:
if infraProvider == "" {
return errNoInfraProvider
}
if bootstrapProvider == "" {
return errNoBootstrapProvider
}
case hmc.TemplateTypeInfraProvider:
if infraProvider == "" {
return errNoInfraProvider
}
case hmc.TemplateTypeBootstrapProvider:
if bootstrapProvider == "" {
return errNoBootstrapProvider
}
case hmc.TemplateTypeManagement:
case hmc.TemplateTypeDeployment, hmc.TemplateTypeProvider, hmc.TemplateTypeManagement:
default:
return errNoProviderType
}
infraProviders := chart.Metadata.Annotations[hmc.ChartAnnotationInfraProviders]
bootstrapProviders := chart.Metadata.Annotations[hmc.ChartAnnotationBootstrapProviders]
cpProviders := chart.Metadata.Annotations[hmc.ChartAnnotationControlPlaneProviders]

template.Status.Type = templateType
template.Status.InfrastructureProvider = infraProvider
template.Status.BootstrapProvider = bootstrapProvider
if infraProviders != "" {
template.Status.InfrastructureProviders = strings.Split(infraProviders, ",")
}
if bootstrapProviders != "" {
template.Status.BootstrapProviders = strings.Split(bootstrapProviders, ",")
}
if cpProviders != "" {
template.Status.ControlPlaneProviders = strings.Split(cpProviders, ",")
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions templates/aws-hosted-cp/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ version: 0.1.0
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
annotations:
hmc.mirantis.com/type: deployment
hmc.mirantis.com/infrastructure-providers: aws
hmc.mirantis.com/controlplane-providers: k0smotron
hmc.mirantis.com/bootstrap-providers: k0s
5 changes: 5 additions & 0 deletions templates/aws-standalone-cp/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ version: 0.1.0
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
annotations:
hmc.mirantis.com/type: deployment
hmc.mirantis.com/infrastructure-providers: aws
hmc.mirantis.com/controlplane-providers: k0s
hmc.mirantis.com/bootstrap-providers: k0s

0 comments on commit d7ab7da

Please sign in to comment.