Skip to content

Commit

Permalink
feat: Add support for HelmRelease and status updates (#8)
Browse files Browse the repository at this point in the history
* feat:added the hr and status updates

* fix: clean up test cases and remove unnecessary delete logic

* refactor: Refactored based on PR feedback

Co-authored-by: Sangeetha Madamanchi <[email protected]>
  • Loading branch information
sangdammad and Sangeetha Madamanchi authored Jul 6, 2022
1 parent 0369716 commit 9562d19
Show file tree
Hide file tree
Showing 23 changed files with 2,064 additions and 1,019 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ RUN go mod download
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY pkg/ pkg/
COPY controllers/ controllers/

# Build
Expand All @@ -23,7 +22,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -a -o ma
# ______________________________________________________________________________________________________________________
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot as prod
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ vet: ## Run go vet against code.
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out


.PHONY: pretest
pretest: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)"
##@ Build

.PHONY: build
Expand All @@ -120,11 +124,7 @@ run: manifests generate fmt vet ## Run a controller from your host.

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} --target prod .

.PHONY: docker-build-debug
docker-build-debug: test ## Build docker image with the manager.
docker build -t ${IMG} --target debug .
docker build -t ${IMG}

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ To create a new CRD with the corresponding Go files:
```console
operator-sdk create api --group core --version v1alpha1 --kind Application --resource=true --controller=true --namespaced=true
```
## Prerequisite
This project is based on golang 1.17. Make sure your GoRoot is configured to 1.17. If any other version then you may face issues.

## Debugging
Since this project is a Kubernetes operator triggered by resource creation, debugging can be done using a remote debugger.
Expand Down
79 changes: 46 additions & 33 deletions api/v1alpha1/application_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ package v1alpha1

import (
"github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/fluxcd/pkg/apis/meta"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type Metadata struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}

type ReleaseTemplate struct {
// Metadata to be applied to the resources created by the Application Controller
// +optional
Metadata `json:"metadata,omitempty"`

// Spec to be applied to the Helm Release resource created by the Application Controller
// +required
Spec v2beta1.HelmReleaseSpec `json:"spec,omitempty"`
}

// ApplicationSpec defines the desired state of Application
type ApplicationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Expand All @@ -36,9 +53,9 @@ type ApplicationSpec struct {
// +optional
PreRenderer PreRenderer `json:"preRenderer,omitempty"`

// Helm Release spec
// Template of Release metadata and spec needed for the resources created by the Application Controller
// +required
Spec v2beta1.HelmReleaseSpec `json:"spec,omitempty"`
Template ReleaseTemplate `json:"template,omitempty"`
}

// ApplicationStatus defines the observed state of Application
Expand All @@ -54,42 +71,18 @@ type ApplicationStatus struct {
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// LastAppliedRevision is the revision of the last successfully applied source.
// +optional
LastAppliedRevision string `json:"lastAppliedRevision,omitempty"`

// LastAttemptedRevision is the revision of the last reconciliation attempt.
// +optional
LastAttemptedRevision string `json:"lastAttemptedRevision,omitempty"`

// LastAttemptedValuesChecksum is the SHA1 checksum of the values of the last
// reconciliation attempt.
// HelmReleaseResourceVersion is the helm release resource version
// +optional
LastAttemptedValuesChecksum string `json:"lastAttemptedValuesChecksum,omitempty"`
HelmReleaseResourceVersion string `json:"helmReleaseResourceVersion,omitempty"`

// LastApplicationRevision is the revision of the last successful Application.
// ValuesResourceVersion is the resource version of the resource that contains the helm values
// +optional
LastReleaseRevision int `json:"lastReleaseRevision,omitempty"`

// HelmChart is the namespaced name of the HelmChart resource created by
// the operator.
// +optional
HelmChart string `json:"helmChart,omitempty"`
ValuesResourceVersion string `json:"valuesResourceVersion,omitempty"`

// Failures is the reconciliation failure count against the latest desired
// state. It is reset after a successful reconciliation.
// +optional
Failures int64 `json:"failures,omitempty"`

// InstallFailures is the install failure count against the latest desired
// state. It is reset after a successful reconciliation.
// +optional
InstallFailures int64 `json:"installFailures,omitempty"`

// UpgradeFailures is the upgrade failure count against the latest desired
// state. It is reset after a successful reconciliation.
// +optional
UpgradeFailures int64 `json:"upgradeFailures,omitempty"`
}

// +genclient
Expand All @@ -105,9 +98,8 @@ type ApplicationStatus struct {
type Application struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ApplicationSpec `json:"spec,omitempty"`
Status ApplicationStatus `json:"status,omitempty"`
Spec ApplicationSpec `json:"spec,omitempty"`
Status ApplicationStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -122,3 +114,24 @@ type ApplicationList struct {
func init() {
SchemeBuilder.Register(&Application{}, &ApplicationList{})
}

func AppInProgressStatus(application *Application) {
application.Status.Conditions = []metav1.Condition{}
condition := metav1.Condition{
Type: meta.ReadyCondition,
Status: metav1.ConditionUnknown,
Reason: meta.ProgressingReason,
Message: "Reconciliation in progress",
}
apimeta.SetStatusCondition(&application.Status.Conditions, condition)
}

func AppErrorStatus(application *Application, error string) {
condition := metav1.Condition{
Type: meta.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: meta.FailedReason,
Message: error,
}
apimeta.SetStatusCondition(&application.Status.Conditions, condition)
}
48 changes: 47 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 9562d19

Please sign in to comment.