Skip to content

Commit

Permalink
Implement json patches for manifests
Browse files Browse the repository at this point in the history
Signed-off-by: alexander-demicev <[email protected]>
  • Loading branch information
alexander-demicev committed Oct 23, 2023
1 parent cf8fdca commit 1c091f2
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 2 deletions.
41 changes: 41 additions & 0 deletions internal/controller/component_patches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 The Kubernetes 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 controller

import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
"sigs.k8s.io/cluster-api-operator/internal/patch"
ctrl "sigs.k8s.io/controller-runtime"
)

func applyPatches(ctx context.Context, provider genericprovider.GenericProvider) func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error) {
log := ctrl.LoggerFrom(ctx)

return func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error) {
if len(provider.GetSpec().ManifestPatches) == 0 {
log.V(5).Info("No resource patches to apply")
return objs, nil
}

log.V(5).Info("Applying resource patches")

return patch.ApplyPatches(objs, provider.GetSpec().ManifestPatches)
}
}
8 changes: 6 additions & 2 deletions internal/controller/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,12 @@ func (p *phaseReconciler) fetch(ctx context.Context) (reconcile.Result, error) {

// ProviderSpec provides fields for customizing the provider deployment options.
// We can use clusterctl library to apply this customizations.
err = repository.AlterComponents(p.components, customizeObjectsFn(p.provider))
if err != nil {
if err := repository.AlterComponents(p.components, customizeObjectsFn(p.provider)); err != nil {
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason)
}

// Apply patches to the provider components if specified.
if err := repository.AlterComponents(p.components, applyPatches(ctx, p.provider)); err != nil {
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason)
}

Expand Down
44 changes: 44 additions & 0 deletions internal/patch/matchinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 The Kubernetes 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 patch

import (
"fmt"

"sigs.k8s.io/yaml"
)

// we match resources and patches on their v1 TypeMeta.
type matchInfo struct {
Kind string `json:"kind,omitempty"`
APIVersion string `json:"apiVersion,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}

type Metadata struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
}

func parseYAMLMatchInfo(raw []byte) (matchInfo, error) {
m := matchInfo{}
if err := yaml.Unmarshal(raw, &m); err != nil {
return matchInfo{}, fmt.Errorf("failed to parse match info: %w", err)
}

return m, nil
}
51 changes: 51 additions & 0 deletions internal/patch/mergepatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 The Kubernetes 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 patch

import (
"fmt"

"sigs.k8s.io/yaml"
)

type mergePatch struct {
json []byte
matchInfo matchInfo
}

func parseMergePatches(rawPatches []string) ([]mergePatch, error) {
patches := []mergePatch{}

for _, patch := range rawPatches {
matchInfo, err := parseYAMLMatchInfo([]byte(patch))
if err != nil {
return nil, fmt.Errorf("failed to parse patch: %w", err)
}

json, err := yaml.YAMLToJSON([]byte(patch))
if err != nil {
return nil, fmt.Errorf("failed to convert YAML to JSON: %w", err)
}

patches = append(patches, mergePatch{
json: json,
matchInfo: matchInfo,
})
}

return patches, nil
}
68 changes: 68 additions & 0 deletions internal/patch/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 The Kubernetes 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 patch

import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
"sigs.k8s.io/yaml"
)

// ApplyPatches patches a list of unstructured objects with a list of patches.
// Patches match if their kind and apiVersion match a document, with the exception
// that if the patch does not set apiVersion it will be ignored.
func ApplyPatches(toPatch []unstructured.Unstructured, patches []string) ([]unstructured.Unstructured, error) {
resources, err := parseResources(toPatch)
if err != nil {
return nil, fmt.Errorf("failed to parse resources: %w", err)
}

mergePatches, err := parseMergePatches(patches)
if err != nil {
return nil, fmt.Errorf("failed to parse patches: %w", err)
}

result := []unstructured.Unstructured{}

for _, r := range resources {
for _, p := range mergePatches {
if _, err := r.applyMergePatch(p); err != nil {
return nil, fmt.Errorf("failed to apply patch: %w", err)
}
}

r.patchedYAML, err = yaml.JSONToYAML(r.json)
if err != nil {
return nil, fmt.Errorf("failed to parse resource: %w", err)
}

patchedObj, err := utilyaml.ToUnstructured(r.patchedYAML)
if err != nil {
return nil, fmt.Errorf("failed to parse resource: %w", err)
}

if len(patchedObj) == 0 {
return nil, fmt.Errorf("patched object is empty")
}

result = append(result, patchedObj...)
}

return result, nil
}
Loading

0 comments on commit 1c091f2

Please sign in to comment.