Skip to content
instance.go 3.24 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

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 meta

import (
	"fmt"

	"k8s.io/apimachinery/pkg/labels"
)

// TODO remove meta in interface, not needed
// Instance interface can work with the commons app.kubernetes.io labels - https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels
type Instance interface {
	Meta
	// The name of the application - app.kubernetes.io/name
	GetApplication() string
	SetApplication(string)
	// A unique name identifying the instance of an application - app.kubernetes.io/instance
	GetInstance() string
	SetInstance(string)
	// The current version of the application (e.g., a semantic version, revision hash, etc.) - app.kubernetes.io/version
	GetVersion() string
	SetVersion(string)
	// The component within the architecture -  app.kubernetes.io/component
	GetComponent() string
	SetComponent(string)
	//  The name of a higher level application this one is part of - app.kubernetes.io/part-of
	GetPartOf() string
	SetPartOf(string)
	// The tool being used to manage the operation of an application - app.kubernetes.io/managed-by
	GetManagedBy() string
	SetManagedBy(string)
}

// Instance labels returns the app.kubernetes.io labels
func InstanceLabels(m Instance) map[string]string {
	labels := labels.Set{
		"app.kubernetes.io/name":       m.GetApplication(),
		"app.kubernetes.io/instance":   m.GetInstance(),
		"app.kubernetes.io/version":    m.GetVersion(),
		"app.kubernetes.io/component":  m.GetComponent(),
		"app.kubernetes.io/part-of":    m.GetPartOf(),
		"app.kubernetes.io/managed-by": m.GetManagedBy(),
	}
	return labels
}

// TODO use Object interface and rename Merge ?
func SetObjectMeta(src Instance, dest Instance) {

	dest.SetLabels(labels.Merge(dest.GetLabels(), InstanceLabels(src)))

	if len(dest.GetName()) == 0 {
		dest.SetName(fmt.Sprintf("%v-%v", src.GetInstance(), dest.GetComponent()))
	}
	if len(dest.GetNamespace()) == 0 {
		dest.SetNamespace(src.GetNamespace())

	}
}
Timothee Gosselin's avatar
Timothee Gosselin committed

func SetObjectMetaFromInstance(src Instance, dest Instance) {

	if len(dest.GetInstance()) == 0 {
		dest.SetInstance(src.GetInstance())
	}

	if len(dest.GetApplication()) == 0 {
		dest.SetApplication(src.GetApplication())
	}

	if len(dest.GetVersion()) == 0 {
		dest.SetVersion(src.GetVersion())
	}

	if len(dest.GetPartOf()) == 0 {
		dest.SetPartOf(src.GetPartOf())
	}

	if len(dest.GetManagedBy()) == 0 {
		dest.SetManagedBy(src.GetManagedBy())
	}

	if len(dest.GetNamespace()) == 0 {
		dest.SetNamespace(src.GetNamespace())

	}
}

func MergeObjectMeta(src Instance, dest Instance) {

	dest.SetLabels(labels.Merge(dest.GetLabels(), InstanceLabels(src)))

	if len(dest.GetName()) == 0 {
		dest.SetName(fmt.Sprintf("%v-%v", src.GetInstance(), dest.GetComponent()))
	}
	if len(dest.GetNamespace()) == 0 {
		dest.SetNamespace(src.GetNamespace())

	}
}