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

feat: helm reference #14

Merged
merged 8 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 14 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: kyma-project.io
layout:
- go.kubebuilder.io/v3
Expand All @@ -11,8 +15,17 @@ resources:
namespaced: true
controller: true
domain: kyma-project.io
group: component
group: operator.kyma-project.io
kind: Sample
path: github.com/kyma-project/template-operator/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: kyma-project.io
group: operator.kyma-project.io
kind: SampleHelm
path: github.com/kyma-project/template-operator/api/v1alpha1
version: v1alpha1
version: "3"
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ Further reading: [Kustomize built-in commonLabels](https://github.com/kubernetes
Include the `State` values in your `Status` sub-resource, either through inline reference or direct inclusion. These values have literal meaning behind them, so use them appropriately.

2. Optionally, you can add additional fields to your `Status` sub-resource.
For instance, `Conditions` are added to `SampleCR` in the [API definition](api/v1alpha1/sample_types.go), along with the required `State` values using inline reference.
3. For instance, `Conditions` are added to `SampleCR` in the [API definition](api/v1alpha1/sample_types.go) and `SampleHelmCR` in the [API definition](api/v1alpha1/samplehelm_types.go).
This also includes the required `State` values, using an inline reference.

<details>
<summary><b>Reference implementation</b></summary>
<summary><b>Reference implementation SampleCR</b></summary>

```go
package v1alpha1
Expand Down Expand Up @@ -201,15 +202,18 @@ _Warning_: This sample implementation is only for reference. You could copy part

1. Implement `State` handling to represent the corresponding state of the reconciled resource, by following [kubebuilder](https://book.kubebuilder.io/) guidelines to implement controllers.

2. Optionally, you could refer to the `SampleCR` [controller implementation](controllers/sample_controller_rendered_resources_test.go) for setting appropriate `State` and `Conditions` values to your `Status` sub-resource, such as:
2. You could refer either to `SampleCR` [controller implementation](controllers/sample_controller_rendered_resources.go) or `SampleHelmCR` [controller implementation](controllers/sample_local_helm_controller.go) for setting appropriate `State` and `Conditions` values to your `Status` sub-resource.

`SampleCR` is reconciled to install / uninstall a list of rendered resources from a YAML file on the file system. Whereas `SampleHelmCR` is reconciled to install / uninstall (using SSA, see next point) rendered resources from a local Helm Chart. The latter uses the Helm library purely to render resources.

````go
r.setStatusForObjectInstance(ctx, objectInstance, status.
WithState(v1alpha1.StateReady).
WithInstallConditionStatus(metav1.ConditionTrue, objectInstance.GetGeneration()))
````

3. This [reference implementation](controllers/sample_controller_rendered_resources_test.go) also covers reading resources from a concatenated YAML file and installing them on the cluster using [Server-side apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/).
Parts of this logic could be leveraged to implement your own controller logic. Checkout functions `getResourcesFromLocalPath()`, `ssa()` and `ssaStatus()` for implementation details.
3. The reference controller implementations listed above use [Server-side apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/) instead of conventional methods to process resources on the target cluster.
Parts of this logic could be leveraged to implement your own controller logic. Checkout functions inside these controllers for state management and other implementation details.

### Local testing
* Connect to your cluster and ensure `kubectl` is pointing to the desired cluster.
Expand Down
96 changes: 96 additions & 0 deletions api/v1alpha1/samplehelm_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"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.

// SampleHelmSpec defines the desired state of SampleHelm
type SampleHelmSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// ChartPath represents the local path to the Helm chart
ChartPath string `json:"chartPath,omitempty"`
}

// SampleHelmStatus defines the observed state of SampleHelm
type SampleHelmStatus struct {
Status `json:",inline"`

// Conditions contain a set of conditionals to determine the State of Status.
// If all Conditions are met, State is expected to be in StateReady.
Conditions []metav1.Condition `json:"conditions,omitempty"`

// add other fields to status subresource here
}

func (s *SampleHelmStatus) WithState(state State) *SampleHelmStatus {
s.State = state
return s
}

func (s *SampleHelmStatus) WithInstallConditionStatus(status metav1.ConditionStatus, objGeneration int64) *SampleHelmStatus {
if s.Conditions == nil {
s.Conditions = make([]metav1.Condition, 0, 1)
}

condition := meta.FindStatusCondition(s.Conditions, ConditionTypeInstallation)

if condition == nil {
condition = &metav1.Condition{
Type: ConditionTypeInstallation,
Reason: ConditionReasonReady,
Message: "installation is ready and resources can be used",
}
}

condition.Status = status
condition.ObservedGeneration = objGeneration
meta.SetStatusCondition(&s.Conditions, *condition)
return s
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// SampleHelm is the Schema for the samplehelms API
type SampleHelm struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec SampleHelmSpec `json:"spec,omitempty"`
Status SampleHelmStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// SampleHelmList contains a list of SampleHelm
type SampleHelmList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []SampleHelm `json:"items"`
}

func init() {
SchemeBuilder.Register(&SampleHelm{}, &SampleHelmList{})
}
97 changes: 97 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.

132 changes: 132 additions & 0 deletions config/crd/bases/operator.kyma-project.io_samplehelms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: samplehelms.operator.kyma-project.io
spec:
group: operator.kyma-project.io
names:
kind: SampleHelm
listKind: SampleHelmList
plural: samplehelms
singular: samplehelm
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: SampleHelm is the Schema for the samplehelms API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: SampleHelmSpec defines the desired state of SampleHelm
properties:
chartPath:
description: ChartPath represents the local path to the Helm chart
type: string
type: object
status:
description: SampleHelmStatus defines the observed state of SampleHelm
properties:
conditions:
description: Conditions contain a set of conditionals to determine
the State of Status. If all Conditions are met, State is expected
to be in StateReady.
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are: \"Available\",
\"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
state:
description: State signifies current state of Module CR. Value can
be one of ("Ready", "Processing", "Error", "Deleting").
enum:
- Processing
- Deleting
- Ready
- Error
type: string
required:
- state
type: object
type: object
served: true
storage: true
subresources:
status: {}
Loading