Skip to content

Commit

Permalink
Merge pull request Mirantis#9 from Kshatrix/templates-controller
Browse files Browse the repository at this point in the history
Add hmc templates controller
  • Loading branch information
Kshatrix authored Jun 4, 2024
2 parents 09657f6 + c91d98f commit eb57839
Show file tree
Hide file tree
Showing 29 changed files with 707 additions and 221 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.21 AS builder
FROM golang:1.22 AS builder
ARG TARGETOS
ARG TARGETARCH

Expand All @@ -14,7 +14,7 @@ RUN go mod download
# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/controller/ internal/controller/
COPY internal/ internal/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
29 changes: 27 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
test: manifests generate fmt vet envtest external-crd ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out

# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
Expand Down Expand Up @@ -133,7 +133,7 @@ endif

.PHONY: deploy-helm-controller
deploy-helm-controller: helm
$(HELM) upgrade --install --set $(FLUX_CHART_VALUES) helm-controller $(FLUX_CHART_REPOSITORY) --version $(FLUX_CHART_VERSION) -n hmc-system
$(HELM) upgrade --install --create-namespace --set $(FLUX_CHART_VALUES) helm-controller $(FLUX_CHART_REPOSITORY) --version $(FLUX_CHART_VERSION) -n hmc-system

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
Expand All @@ -159,6 +159,16 @@ LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

EXTERNAL_CRD_DIR ?= $(LOCALBIN)/crd
$(EXTERNAL_CRD_DIR): $(LOCALBIN)
mkdir -p $(EXTERNAL_CRD_DIR)

FLUX_SOURCE_VERSION ?= $(shell go mod edit -json | jq -r '.Require[] | select(.Path == "github.com/fluxcd/source-controller/api") | .Version')
FLUX_SOURCE_REPO_CRD ?= $(EXTERNAL_CRD_DIR)/source-helmrepositories-$(FLUX_SOURCE_VERSION).yaml
FLUX_SOURCE_CHART_CRD ?= $(EXTERNAL_CRD_DIR)/source-helmchart-$(FLUX_SOURCE_VERSION).yaml
FLUX_HELM_VERSION ?= $(shell go mod edit -json | jq -r '.Require[] | select(.Path == "github.com/fluxcd/helm-controller/api") | .Version')
FLUX_HELM_CRD ?= $(EXTERNAL_CRD_DIR)/helm-$(FLUX_HELM_VERSION).yaml

## Tool Binaries
KUBECTL ?= kubectl
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
Expand Down Expand Up @@ -211,6 +221,21 @@ helmify: $(HELMIFY) ## Download helmify locally if necessary.
$(HELMIFY): $(LOCALBIN)
$(call go-install-tool,$(HELMIFY),github.com/arttor/helmify/cmd/helmify,${HELMIFY_VERSION})

$(FLUX_HELM_CRD): $(EXTERNAL_CRD_DIR)
rm -f $(FLUX_HELM_CRD)
curl -s https://raw.githubusercontent.com/fluxcd/helm-controller/$(FLUX_HELM_VERSION)/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml > $(FLUX_HELM_CRD)

$(FLUX_SOURCE_CHART_CRD): $(EXTERNAL_CRD_DIR)
rm -f $(FLUX_SOURCE_CHART_CRD)
curl -s https://raw.githubusercontent.com/fluxcd/source-controller/$(FLUX_SOURCE_VERSION)/config/crd/bases/source.toolkit.fluxcd.io_helmcharts.yaml > $(FLUX_SOURCE_CHART_CRD)

$(FLUX_SOURCE_REPO_CRD): $(EXTERNAL_CRD_DIR)
rm -f $(FLUX_SOURCE_REPO_CRD)
curl -s https://raw.githubusercontent.com/fluxcd/source-controller/$(FLUX_SOURCE_VERSION)/config/crd/bases/source.toolkit.fluxcd.io_helmrepositories.yaml > $(FLUX_SOURCE_REPO_CRD)

.PHONY: external-crd
external-crd: $(FLUX_HELM_CRD) $(FLUX_SOURCE_CHART_CRD) $(FLUX_SOURCE_REPO_CRD)

# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
// DeploymentSpec defines the desired state of Deployment
type DeploymentSpec struct {
// DryRun specifies whether the template should be applied after validation or only validated.
// +kubebuilder:validation:Optional
DryRun bool `json:"dryRun"`
// +optional
DryRun bool `json:"dryRun,omitempty"`
// Template is a reference to a Template object located in the same namespace.
// +kubebuilder:validation:Required
Template string `json:"template"`
// Configuration allows to provide parameters for template customization.
// If no Configuration provided, the field will be populated with the default values for
// the template and DryRun will be enabled.
// +kubebuilder:validation:Optional
Configuration apiextensionsv1.JSON `json:"configuration"`
// +optional
Configuration apiextensionsv1.JSON `json:"configuration,omitempty"`
}

// DeploymentStatus defines the observed state of Deployment
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ limitations under the License.
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"
)

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

// TemplateSpec defines the desired state of Template
Expand All @@ -26,25 +34,45 @@ type TemplateSpec struct {
// +kubebuilder:validation:Enum=aws
// +kubebuilder:validation:Required
Provider string `json:"provider"`
// HelmChartURL is a URL of the helm chart representing the template.
// Helm holds a reference to a Helm chart representing the HMC template
// +kubebuilder:validation:Required
HelmChartURL string `json:"helmChartURL"`
Helm HelmSpec `json:"helm"`
}

// +kubebuilder:validation:XValidation:rule="(has(self.chartName) && !has(self.chartRef)) || (!has(self.chartName) && has(self.chartRef))", message="either chartName or chartRef must be set"

// HelmSpec references a Helm chart representing the HMC template
type HelmSpec struct {
// ChartName is a name of a Helm chart representing the template in the HMC repository.
// +optional
ChartName string `json:"chartName,omitempty"`
// ChartVersion is a version of a Helm chart representing the template in the HMC repository.
// +optional
ChartVersion string `json:"chartVersion,omitempty"`
// ChartRef is a reference to a source controller resource containing the
// Helm chart representing the template.
// +optional
ChartRef *helmcontrollerv2.CrossNamespaceSourceReference `json:"chartRef,omitempty"`
}

// TemplateStatus defines the observed state of Template
type TemplateStatus struct {
TemplateValidationStatus `json:",inline"`
// Descriptions contains information about the template.
// +optional
Description string `json:"description"`
Description string `json:"description,omitempty"`
// Configuration demonstrates available parameters for template customization,
// that can be used when creating Deployment objects.
// +optional
Configuration apiextensionsv1.JSON `json:"configuration,omitempty"`
}

type TemplateValidationStatus struct {
// Valid indicates whether the template passed validation or not.
Valid bool `json:"valid"`
// ValidationError provides information regarding issues encountered during template validation.
// +optional
ValidationError string `json:"validationError"`
ValidationError string `json:"validationError,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
27 changes: 25 additions & 2 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 charts/hmc/templates/awsprovider-editor-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rules:
- apiGroups:
- hmc.mirantis.com.hmc.mirantis.com
resources:
- awsprovider
- awsproviders
verbs:
- create
- delete
Expand All @@ -20,6 +20,6 @@ rules:
- apiGroups:
- hmc.mirantis.com.hmc.mirantis.com
resources:
- awsprovider/status
- awsproviders/status
verbs:
- get
4 changes: 2 additions & 2 deletions charts/hmc/templates/awsprovider-viewer-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ rules:
- apiGroups:
- hmc.mirantis.com.hmc.mirantis.com
resources:
- awsprovider
- awsproviders
verbs:
- get
- list
- watch
- apiGroups:
- hmc.mirantis.com.hmc.mirantis.com
resources:
- awsprovider/status
- awsproviders/status
verbs:
- get
19 changes: 16 additions & 3 deletions charts/hmc/templates/manager-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rules:
- apiGroups:
- hmc.mirantis.com
resources:
- awsprovider
- awsproviders
verbs:
- create
- delete
Expand All @@ -20,13 +20,13 @@ rules:
- apiGroups:
- hmc.mirantis.com
resources:
- awsprovider/finalizers
- awsproviders/finalizers
verbs:
- update
- apiGroups:
- hmc.mirantis.com
resources:
- awsprovider/status
- awsproviders/status
verbs:
- get
- patch
Expand Down Expand Up @@ -109,6 +109,19 @@ rules:
- get
- patch
- update
- apiGroups:
- source.toolkit.fluxcd.io
resources:
- helmcharts
- helmrepositories
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
58 changes: 53 additions & 5 deletions charts/hmc/templates/template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,71 @@ spec:
spec:
description: TemplateSpec defines the desired state of Template
properties:
helmChartURL:
description: HelmChartURL is a URL of the helm chart representing the
template.
type: string
helm:
description: Helm holds a reference to a Helm chart representing the
HMC template
properties:
chartName:
description: ChartName is a name of a Helm chart representing the
template in the HMC repository.
type: string
chartRef:
description: |-
ChartRef is a reference to a source controller resource containing the
Helm chart representing the template.
properties:
apiVersion:
description: APIVersion of the referent.
type: string
kind:
description: Kind of the referent.
enum:
- OCIRepository
- HelmChart
type: string
name:
description: Name of the referent.
maxLength: 253
minLength: 1
type: string
namespace:
description: |-
Namespace of the referent, defaults to the namespace of the Kubernetes
resource object that contains the reference.
maxLength: 63
minLength: 1
type: string
required:
- kind
- name
type: object
chartVersion:
description: ChartVersion is a version of a Helm chart representing
the template in the HMC repository.
type: string
type: object
x-kubernetes-validations:
- message: either chartName or chartRef must be set
rule: (has(self.chartName) && !has(self.chartRef)) || (!has(self.chartName)
&& has(self.chartRef))
provider:
description: Provider specifies a CAPI provider associated with the
template.
enum:
- aws
type: string
required:
- helmChartURL
- helm
- provider
type: object
status:
description: TemplateStatus defines the observed state of Template
properties:
configuration:
description: |-
Configuration demonstrates available parameters for template customization,
that can be used when creating Deployment objects.
x-kubernetes-preserve-unknown-fields: true
description:
description: Descriptions contains information about the template.
type: string
Expand Down
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

hcv2 "github.com/fluxcd/helm-controller/api/v2"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -48,6 +50,8 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(hmcmirantiscomv1alpha1.AddToScheme(scheme))
utilruntime.Must(sourcev1.AddToScheme(scheme))
utilruntime.Must(hcv2.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
Loading

0 comments on commit eb57839

Please sign in to comment.