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

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

Timothee Gosselin's avatar
Timothee Gosselin committed
    https://www.gnu.org/licenses/agpl-3.0.html
Timothee Gosselin's avatar
Timothee Gosselin committed

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"
)

Timothee Gosselin's avatar
Timothee Gosselin committed
// 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
Timothee Gosselin's avatar
Timothee Gosselin committed
type Instance interface {
	Meta
Timothee Gosselin's avatar
Timothee Gosselin committed
	// The name of the application - app.kubernetes.io/name
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetApplication() string
	SetApplication(string)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// A unique name identifying the instance of an application - app.kubernetes.io/instance
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetInstance() string
	SetInstance(string)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// The current version of the application (e.g., a semantic version, revision hash, etc.) - app.kubernetes.io/version
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetVersion() string
	SetVersion(string)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// The component within the architecture -  app.kubernetes.io/component
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetComponent() string
	SetComponent(string)
Timothee Gosselin's avatar
Timothee Gosselin committed
	//  The name of a higher level application this one is part of - app.kubernetes.io/part-of
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetPartOf() string
	SetPartOf(string)
Timothee Gosselin's avatar
Timothee Gosselin committed
	// The tool being used to manage the operation of an application - app.kubernetes.io/managed-by
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetManagedBy() string
	SetManagedBy(string)
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// Instance labels returns the app.kubernetes.io labels
Timothee Gosselin's avatar
Timothee Gosselin committed
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
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// TODO use Object interface and rename Merge ?
Timothee Gosselin's avatar
Timothee Gosselin committed
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())

	}
}