Skip to content
object.go 6.14 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

Timothee Gosselin's avatar
Timothee Gosselin committed
// ObjectMeta meta is a stripped down version of the ObjectMeta type https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#ObjectMeta
// with only user defined specs
//
// This type can be used in an application CRD
// ObjectMeta implements the Meta interface and can be used to mutates an object
//
Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:object:generate=true
type ObjectMeta struct {
Timothee Gosselin's avatar
Timothee Gosselin committed
	// Name must be unique within a namespace. Is required when creating resources, although
	// some resources may allow a client to request the generation of an appropriate name
	// automatically. Name is primarily intended for creation idempotence and configuration
	// definition.
	// Cannot be updated.
	// More info: http://kubernetes.io/docs/user-guide/identifiers#names
	// +optional
	Name string `json:"name,omitempty"`

	// Namespace defines the space within each name must be unique. An empty namespace is
	// equivalent to the "default" namespace, but "default" is the canonical representation.
	// Not all objects are required to be scoped to a namespace - the value of this field for
	// those objects will be empty.
	//
	// Must be a DNS_LABEL.
	// Cannot be updated.
	// More info: http://kubernetes.io/docs/user-guide/namespaces
	// +optional
	Namespace string `json:"namespace,omitempty"`

	// Map of string keys and values that can be used to organize and categorize
	// (scope and select) objects. May match selectors of replication controllers
	// and services.
	// More info: http://kubernetes.io/docs/user-guide/labels
	// +optional
	Labels map[string]string `json:"labels,omitempty"`

	// Annotations is an unstructured key value map stored with a resource that may be
	// set by external tools to store and retrieve arbitrary metadata. They are not
	// queryable and should be preserved when modifying objects.
	// More info: http://kubernetes.io/docs/user-guide/annotations
	// +optional
Timothee Gosselin's avatar
Timothee Gosselin committed
	Annotations map[string]string `json:"annotations,omitempty"`
}

func (o *ObjectMeta) TemplateValues() map[string]interface{} {
	values := make(map[string]interface{})
	values["name"] = o.Name
	values["namespace"] = o.Namespace
	values["labels"] = o.Labels
	values["annotations"] = o.Annotations
	return values
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetLabels sets the ObjectMeta labels
Timothee Gosselin's avatar
Timothee Gosselin committed
func (c *ObjectMeta) SetLabels(labels map[string]string) {
	if len(c.Labels) == 0 {
		c.Labels = make(map[string]string)
	}
	if len(labels) > 0 {
		for k, v := range labels {
			if len(c.Labels[k]) == 0 {
				c.Labels[k] = v
			}
		}
	}
}

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetAnnotations sets the ObjectMeta annotations
func (c *ObjectMeta) SetAnnotations(annotations map[string]string) {
	if len(c.Annotations) == 0 {
		c.Annotations = make(map[string]string)
	}
	if len(annotations) > 0 {
		for k, v := range annotations {
			if len(c.Annotations[k]) == 0 {
				c.Annotations[k] = v
			}
		}
	}
}

// GetLabels returns the ObjectMeta labels
func (c *ObjectMeta) GetLabels() map[string]string { return c.Labels }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetAnnotations returns the Annotations labels
Timothee Gosselin's avatar
Timothee Gosselin committed
func (c *ObjectMeta) GetAnnotations() map[string]string { return c.Annotations }
Timothee Gosselin's avatar
Timothee Gosselin committed
// GetName returns the ObjectMeta name
func (o *ObjectMeta) GetName() string { return o.Name }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetName sets the ObjectMeta name
func (o *ObjectMeta) SetName(name string) { o.Name = name }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetNamespace returns the ObjectMeta namespace
func (o *ObjectMeta) GetNamespace() string { return o.Namespace }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetNamespace sets the ObjectMeta namespace
func (o *ObjectMeta) SetNamespace(namespace string) { o.Namespace = namespace }
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetApplication returns the ObjectMeta application name from the app.kubernetes.io/name label
Timothee Gosselin's avatar
Timothee Gosselin committed
func (c *ObjectMeta) GetApplication() string { return c.Labels["app.kubernetes.io/name"] }
Timothee Gosselin's avatar
Timothee Gosselin committed
// SetApplication sets the ObjectMeta application name from the app.kubernetes.io/name label
func (c *ObjectMeta) SetApplication(s string) { c.Labels["app.kubernetes.io/name"] = s }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetInstance returns the ObjectMeta application name from the app.kubernetes.io/instance label
func (c *ObjectMeta) GetInstance() string { return c.Labels["app.kubernetes.io/instance"] }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetInstance sets the ObjectMeta application name from the app.kubernetes.io/instance label
func (c *ObjectMeta) SetInstance(s string) { c.Labels["app.kubernetes.io/instance"] = s }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetVersion returns the ObjectMeta application name from the app.kubernetes.io/version label
func (c *ObjectMeta) GetVersion() string { return c.Labels["app.kubernetes.io/version"] }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetVersion sets the ObjectMeta application name from the app.kubernetes.io/version label
func (c *ObjectMeta) SetVersion(s string) { c.Labels["app.kubernetes.io/version"] = s }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetComponent returns the ObjectMeta application name from the app.kubernetes.io/component label
func (c *ObjectMeta) GetComponent() string { return c.Labels["app.kubernetes.io/component"] }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetComponent sets the ObjectMeta application name from the app.kubernetes.io/component label
func (c *ObjectMeta) SetComponent(s string) { c.Labels["app.kubernetes.io/component"] = s }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetPartOf returns the ObjectMeta application name from the app.kubernetes.io/part-of label
func (c *ObjectMeta) GetPartOf() string { return c.Labels["app.kubernetes.io/part-of"] }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetPartOf sets the ObjectMeta application name from the app.kubernetes.io/part-of label
func (c *ObjectMeta) SetPartOf(s string) { c.Labels["app.kubernetes.io/part-of"] = s }

Timothee Gosselin's avatar
Timothee Gosselin committed
// GetManagedBy returns the ObjectMeta application name from the app.kubernetes.io/managed-by label
func (c *ObjectMeta) GetManagedBy() string { return c.Labels["app.kubernetes.io/managed-by"] }

Timothee Gosselin's avatar
Timothee Gosselin committed
// SetManagedBy  sets the ObjectMeta application name from the app.kubernetes.io/managed-by label
func (c *ObjectMeta) SetManagedBy(s string) { c.Labels["app.kubernetes.io/managed-by"] = s }