...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/statuschecktemplate_types.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	"fmt"
    20  
    21  	v1 "k8s.io/api/core/v1"
    22  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    23  )
    24  
    25  const (
    26  	// TemplateTypeLabelKey is a label that represents the template type.
    27  	TemplateTypeLabelKey = "template.chaos-mesh.org/type"
    28  	// ManagedByLabelKey is a label that represents the tool being used
    29  	// to manage the operation of the object.
    30  	ManagedByLabelKey = "app.kubernetes.io/managed-by"
    31  	// ManagedByLabelValue is the value that represents the object is
    32  	// managed by Chaos Mesh.
    33  	ManagedByLabelValue = "chaos-mesh"
    34  
    35  	// TemplateNameAnnotationKey is an annotation that represents
    36  	// the real name of the template.
    37  	TemplateNameAnnotationKey = "template.chaos-mesh.org/name"
    38  	// TemplateDescriptionAnnotationKey is an annotation that represents
    39  	// the description of the template.
    40  	TemplateDescriptionAnnotationKey = "template.chaos-mesh.org/description"
    41  
    42  	// PrefixStatusCheckTemplate is the prefix of the name of a StatusCheckTemplate.
    43  	PrefixStatusCheckTemplate = "template-status-check"
    44  	// StatusCheckTemplateKey is the key that status check spec
    45  	// saved in the template ConfigMap.
    46  	StatusCheckTemplateKey = "spec"
    47  )
    48  
    49  // StatusCheckTemplate represents a template of status check.
    50  // A statusCheckTemplate would save in the ConfigMap named `template-status-check-<template-name>`.
    51  // +kubebuilder:object:generate=false
    52  type StatusCheckTemplate struct {
    53  	StatusCheckSpec `json:",inline"`
    54  }
    55  
    56  func GetTemplateName(cm v1.ConfigMap) string {
    57  	return cm.Annotations[TemplateNameAnnotationKey]
    58  }
    59  
    60  func GetTemplateDescription(cm v1.ConfigMap) string {
    61  	return cm.Annotations[TemplateDescriptionAnnotationKey]
    62  }
    63  
    64  func GenerateTemplateName(name string) string {
    65  	return fmt.Sprintf("%s-%s", PrefixStatusCheckTemplate, name)
    66  }
    67  
    68  func IsStatusCheckTemplate(cm v1.ConfigMap) bool {
    69  	return cm.Labels[ManagedByLabelKey] == ManagedByLabelValue &&
    70  		cm.Labels[TemplateTypeLabelKey] == KindStatusCheck &&
    71  		cm.Name == GenerateTemplateName(cm.Annotations[TemplateNameAnnotationKey])
    72  }
    73  
    74  func (in *StatusCheckTemplate) Validate() (admission.Warnings, error) {
    75  	statusCheck := &StatusCheck{
    76  		Spec: in.StatusCheckSpec,
    77  	}
    78  	return statusCheck.Validate()
    79  }
    80  
    81  func (in *StatusCheckTemplate) Default() {
    82  	if in == nil {
    83  		return
    84  	}
    85  
    86  	statusCheck := &StatusCheck{
    87  		Spec: in.StatusCheckSpec,
    88  	}
    89  	statusCheck.Default()
    90  	in.StatusCheckSpec = *statusCheck.Spec.DeepCopy()
    91  }
    92