mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 18:01:21 +00:00
Reduce indirection.
This commit is contained in:
59
pkg/resource/generationbehavior.go
Normal file
59
pkg/resource/generationbehavior.go
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright 2018 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 resource
|
||||
|
||||
// GenerationBehavior specifies generation behavior of configmaps, secrets and maybe other resources.
|
||||
type GenerationBehavior int
|
||||
|
||||
const (
|
||||
// Unspecified behavior typically treated as a Create.
|
||||
BehaviorUnspecified GenerationBehavior = iota
|
||||
// Make a new resource.
|
||||
BehaviorCreate
|
||||
// Replace a resource.
|
||||
BehaviorReplace
|
||||
// Attempt to merge a new resource with an existing resource.
|
||||
BehaviorMerge
|
||||
)
|
||||
|
||||
// String converts a GenerationBehavior to a string.
|
||||
func (b GenerationBehavior) String() string {
|
||||
switch b {
|
||||
case BehaviorReplace:
|
||||
return "replace"
|
||||
case BehaviorMerge:
|
||||
return "merge"
|
||||
case BehaviorCreate:
|
||||
return "create"
|
||||
default:
|
||||
return "unspecified"
|
||||
}
|
||||
}
|
||||
|
||||
// NewGenerationBehavior converts a string to a GenerationBehavior.
|
||||
func NewGenerationBehavior(s string) GenerationBehavior {
|
||||
switch s {
|
||||
case "replace":
|
||||
return BehaviorReplace
|
||||
case "merge":
|
||||
return BehaviorMerge
|
||||
case "create":
|
||||
return BehaviorCreate
|
||||
default:
|
||||
return BehaviorUnspecified
|
||||
}
|
||||
}
|
||||
@@ -25,56 +25,49 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// Resource is a Kubernetes Resource Object paired with a behavior.
|
||||
// Resource is an "Unstructured" (json/map form) Kubernetes API resource object
|
||||
// paired with a GenerationBehavior.
|
||||
type Resource struct {
|
||||
unstruct *unstructured.Unstructured
|
||||
behavior string
|
||||
unstructured.Unstructured
|
||||
b GenerationBehavior
|
||||
}
|
||||
|
||||
// NewResource returns a new instance of Resource.
|
||||
func NewResource(u *unstructured.Unstructured, b string) *Resource {
|
||||
return &Resource{unstruct: u, behavior: b}
|
||||
// NewResourceWithBehavior returns a new instance of Resource.
|
||||
func NewResourceWithBehavior(obj runtime.Object, b GenerationBehavior) (*Resource, error) {
|
||||
// Convert obj to a byte stream, then convert that to JSON (Unstructured).
|
||||
marshaled, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var u unstructured.Unstructured
|
||||
err = u.UnmarshalJSON(marshaled)
|
||||
return &Resource{Unstructured: u, b: b}, nil
|
||||
}
|
||||
|
||||
// NewBehaviorlessResource returns a new instance of Resource.
|
||||
func NewBehaviorlessResource(u *unstructured.Unstructured) *Resource {
|
||||
return &Resource{unstruct: u}
|
||||
return &Resource{Unstructured: *u, b: BehaviorUnspecified}
|
||||
}
|
||||
|
||||
// Behavior returns the behavior for the resource.
|
||||
func (r *Resource) Behavior() string {
|
||||
return r.behavior
|
||||
}
|
||||
|
||||
// Unstruct returns the unstructured object holding the resource.
|
||||
func (r *Resource) Unstruct() *unstructured.Unstructured {
|
||||
return r.unstruct
|
||||
}
|
||||
|
||||
// SetUnstruct sets a new member.
|
||||
func (r *Resource) SetUnstruct(u *unstructured.Unstructured) {
|
||||
r.unstruct = u
|
||||
func (r *Resource) Behavior() GenerationBehavior {
|
||||
return r.b
|
||||
}
|
||||
|
||||
// Id returns the ResId for the resource.
|
||||
func (r *Resource) Id() ResId {
|
||||
var empty ResId
|
||||
if r.unstruct == nil {
|
||||
return empty
|
||||
}
|
||||
gvk := r.unstruct.GroupVersionKind()
|
||||
return NewResId(gvk, r.unstruct.GetName())
|
||||
return NewResId(r.GroupVersionKind(), r.GetName())
|
||||
}
|
||||
|
||||
func (r *Resource) Merge(other *Resource) {
|
||||
r.Replace(other)
|
||||
mergeConfigmap(r.unstruct.Object, other.unstruct.Object, r.unstruct.Object)
|
||||
mergeConfigmap(r.Object, other.Object, r.Object)
|
||||
}
|
||||
|
||||
func (r *Resource) Replace(other *Resource) {
|
||||
r.unstruct.SetLabels(mergeStringMaps(other.unstruct.GetLabels(), r.unstruct.GetLabels()))
|
||||
r.unstruct.SetAnnotations(mergeStringMaps(other.unstruct.GetAnnotations(), r.unstruct.GetAnnotations()))
|
||||
r.unstruct.SetName(other.unstruct.GetName())
|
||||
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
|
||||
r.SetAnnotations(mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
|
||||
r.SetName(other.GetName())
|
||||
}
|
||||
|
||||
// TODO: Add BinaryData once we sync to new k8s.io/api
|
||||
|
||||
Reference in New Issue
Block a user