generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement json patches for manifests
Signed-off-by: alexander-demicev <[email protected]>
- Loading branch information
1 parent
603f052
commit bbedbce
Showing
7 changed files
with
503 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 parse patch: %w", err) | ||
} | ||
|
||
patches = append(patches, mergePatch{ | ||
json: json, | ||
matchInfo: matchInfo, | ||
}) | ||
} | ||
|
||
return patches, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("failted 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 | ||
} |
Oops, something went wrong.