Skip to content

Commit

Permalink
Merge pull request K0rdent#370 from Mirantis/mgmt-release
Browse files Browse the repository at this point in the history
Support Release objects in Management api
  • Loading branch information
Kshatrix authored Sep 23, 2024
2 parents cfad64f + f4d0092 commit 07398dc
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 143 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ set-hmc-version: yq
$(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
$(YQ) eval '.spec.version = "$(VERSION)"' -i $(PROVIDER_TEMPLATES_DIR)/hmc-templates/files/release.yaml
$(YQ) eval '.metadata.name = "hmc-$(patsubst v%,%,$(subst .,-, $(VERSION)))"' -i $(PROVIDER_TEMPLATES_DIR)/hmc-templates/files/release.yaml

.PHONY: hmc-chart-release
hmc-chart-release: set-hmc-version templates-generate ## Generate hmc helm chart
Expand Down Expand Up @@ -295,6 +297,10 @@ dev-push: docker-build helm-push
dev-templates: templates-generate
$(KUBECTL) -n $(NAMESPACE) apply -f $(PROVIDER_TEMPLATES_DIR)/hmc-templates/files/templates

.PHONY: dev-release
dev-release:
@$(YQ) e ".spec.version = \"${VERSION}\"" $(PROVIDER_TEMPLATES_DIR)/hmc-templates/files/release.yaml | $(KUBECTL) -n $(NAMESPACE) apply -f -

.PHONY: dev-aws-creds
dev-aws-creds: envsubst
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -no-unset -i config/dev/aws-credentials.yaml | $(KUBECTL) apply -f -
Expand All @@ -308,7 +314,7 @@ dev-vsphere-creds: envsubst
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -no-unset -i config/dev/vsphere-credentials.yaml | $(KUBECTL) apply -f -

.PHONY: dev-apply ## Apply the development environment by deploying the kind cluster, local registry and the HMC helm chart.
dev-apply: kind-deploy registry-deploy dev-push dev-deploy dev-templates
dev-apply: kind-deploy registry-deploy dev-push dev-deploy dev-templates dev-release

.PHONY: dev-destroy
dev-destroy: kind-undeploy registry-undeploy ## Destroy the development environment by deleting the kind cluster and local registry.
Expand Down
64 changes: 54 additions & 10 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

package v1alpha1

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Providers is a structure holding different types of CAPI providers
type Providers struct {
// InfrastructureProviders is the list of CAPI infrastructure providers
Expand All @@ -38,14 +45,51 @@ const (
ProviderSveltosCreateNamespace = true
)

var (
// DefaultProviders is a map of providers that are
// installed by default, each with its default config.
DefaultProviders = map[string]map[string]interface{}{
ProviderCAPAName: nil,
ProviderAzureName: nil,
ProviderK0smotronName: nil,
ProviderVSphereName: nil,
ProviderSveltosName: nil,
func SetupIndexers(ctx context.Context, mgr ctrl.Manager) error {
if err := SetupManagedClusterIndexer(ctx, mgr); err != nil {
return err
}
)
if err := SetupReleaseIndexer(ctx, mgr); err != nil {
return err
}

return nil
}

const TemplateKey = ".spec.template"

func SetupManagedClusterIndexer(ctx context.Context, mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().
IndexField(ctx, &ManagedCluster{}, TemplateKey, ExtractTemplateName); err != nil {
return err
}

return nil
}

func ExtractTemplateName(rawObj client.Object) []string {
cluster, ok := rawObj.(*ManagedCluster)
if !ok {
return nil
}
return []string{cluster.Spec.Template}
}

const VersionKey = ".spec.version"

func SetupReleaseIndexer(ctx context.Context, mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().
IndexField(ctx, &Release{}, VersionKey, ExtractReleaseVersion); err != nil {
return err
}

return nil
}

func ExtractReleaseVersion(rawObj client.Object) []string {
release, ok := rawObj.(*Release)
if !ok {
return nil
}
return []string{release.Spec.Version}
}
65 changes: 16 additions & 49 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,24 @@
package v1alpha1

import (
"encoding/json"
"fmt"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)

const (
CoreHMCName = "hmc"
DefaultCoreHMCTemplate = "hmc"
CoreHMCName = "hmc"

CoreCAPIName = "capi"
DefaultCoreCAPITemplate = "cluster-api"
CoreCAPIName = "capi"

ManagementName = "hmc"
ManagementFinalizer = "hmc.mirantis.com/management"
)

var DefaultCoreConfiguration = Core{
HMC: Component{
Template: DefaultCoreHMCTemplate,
},
CAPI: Component{
Template: DefaultCoreCAPITemplate,
},
}

// ManagementSpec defines the desired state of Management
type ManagementSpec struct {
// Release references the Release object.
Release string `json:"release"`
// Core holds the core Management components that are mandatory.
// If not specified, will be populated with the default values.
Core *Core `json:"core,omitempty"`
Expand All @@ -56,15 +44,16 @@ type ManagementSpec struct {
// Core represents a structure describing core Management components.
type Core struct {
// HMC represents the core HMC component and references the HMC template.
HMC Component `json:"hmc"`
HMC Component `json:"hmc,omitempty"`
// CAPI represents the core Cluster API component and references the Cluster API template.
CAPI Component `json:"capi"`
CAPI Component `json:"capi,omitempty"`
}

// Component represents HMC management component
type Component struct {
// Template is the name of the Template associated with this component.
Template string `json:"template"`
// If not specified, will be taken from the Release object.
Template string `json:"template,omitempty"`
// Config allows to provide parameters for management component customization.
// If no Config provided, the field will be populated with the default
// values for the template.
Expand All @@ -85,38 +74,14 @@ func (in *Component) HelmValues() (values map[string]interface{}, err error) {
return values, err
}

func (m *ManagementSpec) SetDefaults() bool {
if m.Core != nil {
return false
}
m.Core = &DefaultCoreConfiguration
return true
}

func (m *ManagementSpec) SetProvidersDefaults() error {
providers := []Provider{}

for name, cfg := range DefaultProviders {
c := Provider{
Name: name,
Component: Component{
Template: name,
},
}

if len(cfg) > 0 {
b, err := json.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to marshal config for %s provider: %w", name, err)
}

c.Config = &apiextensionsv1.JSON{Raw: b}
}

providers = append(providers, c)
m.Providers = []Provider{
{Name: ProviderK0smotronName},
{Name: ProviderCAPAName},
{Name: ProviderAzureName},
{Name: ProviderVSphereName},
{Name: ProviderSveltosName},
}

m.Providers = providers
return nil
}

Expand All @@ -133,6 +98,8 @@ type ManagementStatus struct {

// ComponentStatus is the status of Management component installation
type ComponentStatus struct {
// Template is the name of the Template associated with this component.
Template string `json:"template,omitempty"`
// Success represents if a component installation was successful
Success bool `json:"success,omitempty"`
// Error stores as error message in case of failed installation
Expand Down
26 changes: 25 additions & 1 deletion api/v1alpha1/release_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,32 @@ type ReleaseSpec struct {
Version string `json:"version"`
// UpgradeableVersions contains a list of versions available to upgrade from.
UpgradeableVersions []string `json:"upgradeableVersions,omitempty"`
// HMC references the HMC template.
HMC CoreProviderTemplate `json:"hmc"`
// CAPI references the Cluster API template.
CAPI CoreProviderTemplate `json:"capi"`
// Providers contains a list of Providers associated with the Release.
Providers []Provider `json:"providers,omitempty"`
Providers []NamedProviderTemplate `json:"providers,omitempty"`
}

type CoreProviderTemplate struct {
// Template references the Template associated with the provider.
Template string `json:"template"`
}

type NamedProviderTemplate struct {
CoreProviderTemplate `json:",inline"`
// Name of the provider.
Name string `json:"name"`
}

func (in *Release) ProviderTemplate(name string) string {
for _, p := range in.Spec.Providers {
if p.Name == name {
return p.Template
}
}
return ""
}

// ReleaseStatus defines the observed state of Release
Expand Down
39 changes: 35 additions & 4 deletions api/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func main() {
}

ctx := ctrl.SetupSignalHandler()
if err = hmcwebhook.SetupTemplateIndex(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create template index", "index", "ClusterTemplate")
if err = hmcmirantiscomv1alpha1.SetupIndexers(ctx, mgr); err != nil {
setupLog.Error(err, "unable to setup indexers")
os.Exit(1)
}

Expand Down
Loading

0 comments on commit 07398dc

Please sign in to comment.