Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] Controller states4 #52

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: k3d-import-img
k3d-import-img:
k3d image import $(IMG) -c $(K3D_CLUSTER_NAME)

.PHONY: apply-sample-cr
apply-sample-cr:
kubectl apply -f config/samples/infrastructuremanager_v1_gardenercluster.yaml

.PHONE: local-build-and-deploy
local-build-and-deploy: docker-build k3d-import-img deploy gardener-secret-deploy apply-sample-cr

.PHONY: local-rebuild-and-redeploy
local-rebuild-and-redeploy: undeploy local-build-and-deploy
##@ Build Dependencies

## Location to install dependencies to
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ make docker-build
k3d
</summary>


```bash
k3d cluster create $K3D_CLUSTER_NAME
k3d image import $IMG -c $K3D_CLUSTER_NAME
```
</details>
Expand Down
28 changes: 28 additions & 0 deletions api/v1/gardenercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1

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

Expand Down Expand Up @@ -73,6 +74,21 @@ const (
DeletingState State = "Deleting"
)

type ConditionReason string

const (
ConditionReasonSecretCreated State = "SecretCreated"
ConditionReasonSecretNotFound State = "SecretNotFound"
ConditionReasonGardenClusterNotRetrieved State = "GardenClusterNotRetrieved"
ConditionReasonGardenClusterNotFound State = "GardenClusterNotFound"
)

type ConditionType string

const (
ConditionTypeUnknown State = "Unknown"
)

// GardenerClusterStatus defines the observed state of GardenerCluster
type GardenerClusterStatus struct {
// State signifies current state of Gardener Cluster.
Expand All @@ -86,6 +102,18 @@ type GardenerClusterStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

func (cluster *GardenerCluster) UpdateState(r State, s State, msg string) {
cluster.Status.State = s
condition := metav1.Condition{
Type: string(ConditionTypeUnknown),
Status: "True",
LastTransitionTime: metav1.Now(),
Reason: string(r),
Message: msg,
}
meta.SetStatusCondition(&cluster.Status.Conditions, condition)
}

func init() {
SchemeBuilder.Register(&GardenerCluster{}, &GardenerClusterList{})
}
5 changes: 3 additions & 2 deletions config/default/manager_gardener_secret_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ spec:
- name: manager
command:
- /manager
imagePullPolicy: Always
args:
- --gardener-kubeconfig-path=/gardener/credentials/kubeconfig
- --gardener-project-name=kyma-dev
- --kubeconfig-expiration-time=24h
- --gardener-project-name=frog-dev
- --kubeconfig-expiration-time=10m
volumeMounts:
- name: gardener-kubeconfig
mountPath: /gardener/credentials
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: infrastructure-manager
newTag: 0.0.1
newName: europe-docker.pkg.dev/kyma-project/dev/infrastructure-manager
newTag: PR-49
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ kind: GardenerCluster
metadata:
labels:
kyma-project.io/instance-id: instance-id
kyma-project.io/runtime-id: runtime-id
kyma-project.io/runtime-id: md-id
kyma-project.io/broker-plan-id: plan-id
kyma-project.io/broker-plan-name: plan-name
kyma-project.io/global-account-id: global-account-id
kyma-project.io/global-account-id: global-account-i
kyma-project.io/subaccount-id: subAccount-id
kyma-project.io/shoot-name: shoot-name
kyma-project.io/shoot-name: md-im
kyma-project.io/region: region
operator.kyma-project.io/kyma-name: kymaName
name: runtime-id
name: md-id
namespace: kcp-system
spec:
shoot:
name: shoot-name
name: md-im
kubeconfig:
secret:
name: kubeconfig-runtime-id
name: kubeconfig-md-im
namespace: kcp-system
key: "config"
10 changes: 10 additions & 0 deletions internal/controller/gardener_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@

err := r.Client.Get(ctx, req.NamespacedName, &cluster)
if err != nil {
cluster.UpdateState(infrastructuremanagerv1.ErrorState, infrastructuremanagerv1.ConditionReasonGardenClusterNotRetrieved, "Couldn't retrieve the Garden Cluster CR")

if k8serrors.IsNotFound(err) {
cluster.UpdateState(infrastructuremanagerv1.DeletingState, infrastructuremanagerv1.ConditionReasonGardenClusterNotFound, "CR not found, secret will be deleted")
err = r.deleteSecret(req.NamespacedName.Name)
if err != nil {
r.log.Error(err, "failed to delete secret")
Expand All @@ -95,7 +98,10 @@

secret, err := r.getSecret(cluster.Spec.Shoot.Name)
if err != nil {
r.log.Error(err, "could not get the Secret for "+cluster.Spec.Shoot.Name)
cluster.UpdateState(infrastructuremanagerv1.ErrorState, infrastructuremanagerv1.ConditionReasonSecretNotFound, "Secret not found, and will be created")
if !k8serrors.IsNotFound(err) {

Check failure on line 103 in internal/controller/gardener_cluster_controller.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

return ctrl.Result{
Requeue: true,
RequeueAfter: defaultRequeuInSeconds,
Expand All @@ -104,12 +110,16 @@
}

if secret == nil {
r.log.Error(err, "Secret not found, and will be created")
cluster.UpdateState(infrastructuremanagerv1.ErrorState, infrastructuremanagerv1.ConditionReasonSecretNotFound, "Secret not found, and will be created")
err = r.createSecret(ctx, cluster)
if err != nil {
return r.ResultWithoutRequeue(), err
}

Check failure on line 119 in internal/controller/gardener_cluster_controller.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

cluster.UpdateState(infrastructuremanagerv1.ConditionReasonSecretCreated, infrastructuremanagerv1.ReadyState, "GardenCluster is ready")
return ctrl.Result{}, nil
}

Expand Down
Loading