Skip to content

Commit

Permalink
Sort manifests, options to create ns & use non-strict engine for helm…
Browse files Browse the repository at this point in the history
… addons

Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Mar 11, 2024
1 parent 8c5a456 commit 479bd02
Show file tree
Hide file tree
Showing 15 changed files with 1,222 additions and 30 deletions.
26 changes: 21 additions & 5 deletions pkg/addonfactory/addonfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ type AgentAddonFactory struct {
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
// trimCRDDescription flag is used to trim the description of CRDs in manifestWork. disabled by default.
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
createAgentInstallNamespace bool
helmEngineStrict bool
}

// NewAgentAddonFactory builds an addonAgentFactory instance with addon name and fs.
Expand All @@ -57,8 +59,10 @@ func NewAgentAddonFactory(addonName string, fs embed.FS, dir string) *AgentAddon
HealthProber: nil,
SupportedConfigGVRs: []schema.GroupVersionResource{},
},
trimCRDDescription: false,
scheme: s,
trimCRDDescription: false,
scheme: s,
createAgentInstallNamespace: false,
helmEngineStrict: false,
}
}

Expand Down Expand Up @@ -115,6 +119,18 @@ func (f *AgentAddonFactory) WithTrimCRDDescription() *AgentAddonFactory {
return f
}

// WithCreateAgentInstallNamespace is to create the agent install namespace object in manifestWork.
func (f *AgentAddonFactory) WithCreateAgentInstallNamespace() *AgentAddonFactory {
f.createAgentInstallNamespace = true
return f
}

// WithHelmEngineStrict is to enable script go template rendering for Helm charts to generate manifestWork.
func (f *AgentAddonFactory) WithHelmEngineStrict() *AgentAddonFactory {
f.helmEngineStrict = true
return f
}

// WithConfigGVRs defines the addon supported configuration GroupVersionResource
func (f *AgentAddonFactory) WithConfigGVRs(gvrs ...schema.GroupVersionResource) *AgentAddonFactory {
f.agentAddonOptions.SupportedConfigGVRs = append(f.agentAddonOptions.SupportedConfigGVRs, gvrs...)
Expand Down
127 changes: 102 additions & 25 deletions pkg/addonfactory/helm_agentaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/releaseutil"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -42,28 +45,59 @@ type helmDefaultValues struct {
}

type HelmAgentAddon struct {
decoder runtime.Decoder
chart *chart.Chart
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
decoder runtime.Decoder
chart *chart.Chart
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
createAgentInstallNamespace bool
helmEngineStrict bool
}

func newHelmAgentAddon(factory *AgentAddonFactory, chart *chart.Chart) *HelmAgentAddon {
return &HelmAgentAddon{
decoder: serializer.NewCodecFactory(factory.scheme).UniversalDeserializer(),
chart: chart,
getValuesFuncs: factory.getValuesFuncs,
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
hostingCluster: factory.hostingCluster,
agentInstallNamespace: factory.agentInstallNamespace,
decoder: serializer.NewCodecFactory(factory.scheme).UniversalDeserializer(),
chart: chart,
getValuesFuncs: factory.getValuesFuncs,
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
hostingCluster: factory.hostingCluster,
agentInstallNamespace: factory.agentInstallNamespace,
createAgentInstallNamespace: factory.createAgentInstallNamespace,
helmEngineStrict: factory.helmEngineStrict,
}
}

func (a *HelmAgentAddon) Manifests(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
objects, err := a.renderManifests(cluster, addon)
if err != nil {
return nil, err
}

manifests := make([]Manifest, 0, len(objects))
for _, obj := range objects {
a, err := meta.TypeAccessor(obj)
if err != nil {
return nil, err
}
manifests = append(manifests, Manifest{
Object: obj,
Kind: a.GetKind(),
})
}
sortManifestsByKind(manifests, releaseutil.InstallOrder)

for i, manifest := range manifests {
objects[i] = manifest.Object
}
return objects, nil
}

func (a *HelmAgentAddon) renderManifests(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
var objects []runtime.Object
Expand All @@ -74,7 +108,7 @@ func (a *HelmAgentAddon) Manifests(
}

helmEngine := engine.Engine{
Strict: true,
Strict: a.helmEngineStrict,
LintMode: false,
}

Expand All @@ -93,16 +127,8 @@ func (a *HelmAgentAddon) Manifests(
return objects, err
}

// sort the filenames of the templates so the manifests are ordered consistently
keys := make([]string, 0, len(templates))
for k := range templates {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
data := templates[k]

agentInstallNamespace := a.getValueAgentInstallNamespace(addon)
for k, data := range templates {
if len(data) == 0 {
continue
}
Expand Down Expand Up @@ -132,6 +158,13 @@ func (a *HelmAgentAddon) Manifests(
}
}

if agentInstallNamespace != "" && a.createAgentInstallNamespace {
var ns unstructured.Unstructured
ns.SetAPIVersion("v1")
ns.SetKind("Namespace")
ns.SetName(agentInstallNamespace)
objects = append(objects, &ns)
}
}

if a.trimCRDDescription {
Expand Down Expand Up @@ -260,3 +293,47 @@ func (a *HelmAgentAddon) releaseOptions(
Namespace: a.getValueAgentInstallNamespace(addon),
}
}

// Manifest represents a manifest file, which has a name and some content.
type Manifest struct {
Object runtime.Object
Kind string
}

// sort manifests by kind.
//
// Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering releaseutil.KindSortOrder) []Manifest {
sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Kind, manifests[j].Kind, ordering)
})

return manifests
}

func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o releaseutil.KindSortOrder) bool {
ordering := make(map[string]int, len(o))
for v, k := range o {
ordering[k] = v
}

first, aok := ordering[kindA]
second, bok := ordering[kindB]

if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB {
return kindA < kindB
}
return first < second
}
// unknown kind is last
if !aok {
return false
}
if !bok {
return true
}
// sort different kinds, keep original order if same priority
return first < second
}
106 changes: 106 additions & 0 deletions vendor/helm.sh/helm/v3/pkg/release/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright The Helm Authors.
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 release

import (
"helm.sh/helm/v3/pkg/time"
)

// HookEvent specifies the hook event
type HookEvent string

// Hook event types
const (
HookPreInstall HookEvent = "pre-install"
HookPostInstall HookEvent = "post-install"
HookPreDelete HookEvent = "pre-delete"
HookPostDelete HookEvent = "post-delete"
HookPreUpgrade HookEvent = "pre-upgrade"
HookPostUpgrade HookEvent = "post-upgrade"
HookPreRollback HookEvent = "pre-rollback"
HookPostRollback HookEvent = "post-rollback"
HookTest HookEvent = "test"
)

func (x HookEvent) String() string { return string(x) }

// HookDeletePolicy specifies the hook delete policy
type HookDeletePolicy string

// Hook delete policy types
const (
HookSucceeded HookDeletePolicy = "hook-succeeded"
HookFailed HookDeletePolicy = "hook-failed"
HookBeforeHookCreation HookDeletePolicy = "before-hook-creation"
)

func (x HookDeletePolicy) String() string { return string(x) }

// HookAnnotation is the label name for a hook
const HookAnnotation = "helm.sh/hook"

// HookWeightAnnotation is the label name for a hook weight
const HookWeightAnnotation = "helm.sh/hook-weight"

// HookDeleteAnnotation is the label name for the delete policy for a hook
const HookDeleteAnnotation = "helm.sh/hook-delete-policy"

// Hook defines a hook object.
type Hook struct {
Name string `json:"name,omitempty"`
// Kind is the Kubernetes kind.
Kind string `json:"kind,omitempty"`
// Path is the chart-relative path to the template.
Path string `json:"path,omitempty"`
// Manifest is the manifest contents.
Manifest string `json:"manifest,omitempty"`
// Events are the events that this hook fires on.
Events []HookEvent `json:"events,omitempty"`
// LastRun indicates the date/time this was last run.
LastRun HookExecution `json:"last_run,omitempty"`
// Weight indicates the sort order for execution among similar Hook type
Weight int `json:"weight,omitempty"`
// DeletePolicies are the policies that indicate when to delete the hook
DeletePolicies []HookDeletePolicy `json:"delete_policies,omitempty"`
}

// A HookExecution records the result for the last execution of a hook for a given release.
type HookExecution struct {
// StartedAt indicates the date/time this hook was started
StartedAt time.Time `json:"started_at,omitempty"`
// CompletedAt indicates the date/time this hook was completed.
CompletedAt time.Time `json:"completed_at,omitempty"`
// Phase indicates whether the hook completed successfully
Phase HookPhase `json:"phase"`
}

// A HookPhase indicates the state of a hook execution
type HookPhase string

const (
// HookPhaseUnknown indicates that a hook is in an unknown state
HookPhaseUnknown HookPhase = "Unknown"
// HookPhaseRunning indicates that a hook is currently executing
HookPhaseRunning HookPhase = "Running"
// HookPhaseSucceeded indicates that hook execution succeeded
HookPhaseSucceeded HookPhase = "Succeeded"
// HookPhaseFailed indicates that hook execution failed
HookPhaseFailed HookPhase = "Failed"
)

// String converts a hook phase to a printable string
func (x HookPhase) String() string { return string(x) }
40 changes: 40 additions & 0 deletions vendor/helm.sh/helm/v3/pkg/release/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright The Helm Authors.
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 release

import (
"k8s.io/apimachinery/pkg/runtime"

"helm.sh/helm/v3/pkg/time"
)

// Info describes release information.
type Info struct {
// FirstDeployed is when the release was first deployed.
FirstDeployed time.Time `json:"first_deployed,omitempty"`
// LastDeployed is when the release was last deployed.
LastDeployed time.Time `json:"last_deployed,omitempty"`
// Deleted tracks when this object was deleted.
Deleted time.Time `json:"deleted"`
// Description is human-friendly "log entry" about this release.
Description string `json:"description,omitempty"`
// Status is the current state of the release
Status Status `json:"status,omitempty"`
// Contains the rendered templates/NOTES.txt if available
Notes string `json:"notes,omitempty"`
// Contains the deployed resources information
Resources map[string][]runtime.Object `json:"resources,omitempty"`
}
Loading

0 comments on commit 479bd02

Please sign in to comment.