How to use NewSpec method of configmap Package

Best Testkube code snippet using configmap.NewSpec

handlers.go

Source:handlers.go Github

copy

Full Screen

1package tfoperator2import (3 "context"4 "fmt"5 "reflect"6 analyticsv1alpha1 "github.com/Svimba/tungstenfabric-operator/pkg/apis/analytics/v1alpha1"7 configv1alpha1 "github.com/Svimba/tungstenfabric-operator/pkg/apis/config/v1alpha1"8 controlv1alpha1 "github.com/Svimba/tungstenfabric-operator/pkg/apis/control/v1alpha1"9 corev1 "k8s.io/api/core/v1"10 "k8s.io/apimachinery/pkg/api/errors"11 "k8s.io/apimachinery/pkg/types"12 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"13)14func (r *ReconcileTFOperator) updateSpec(newSpec *configv1alpha1.TFConfigSpec, curSpec *configv1alpha1.TFConfigSpec) bool {15 changed := false16 if !reflect.DeepEqual(newSpec.APISpec.EnvList, curSpec.APISpec.EnvList) {17 curSpec.APISpec.EnvList = newSpec.APISpec.EnvList18 changed = true19 }20 if !reflect.DeepEqual(newSpec.SVCMonitorSpec.EnvList, curSpec.SVCMonitorSpec.EnvList) {21 curSpec.SVCMonitorSpec.EnvList = newSpec.SVCMonitorSpec.EnvList22 changed = true23 }24 if !reflect.DeepEqual(newSpec.SchemaSpec.EnvList, curSpec.SchemaSpec.EnvList) {25 curSpec.SchemaSpec.EnvList = newSpec.SchemaSpec.EnvList26 changed = true27 }28 if !reflect.DeepEqual(newSpec.DeviceMgrSpec.EnvList, curSpec.DeviceMgrSpec.EnvList) {29 curSpec.DeviceMgrSpec.EnvList = newSpec.DeviceMgrSpec.EnvList30 changed = true31 }32 return changed33}34// Config Operator handler35// return true/false(Requeue), error36func (r *ReconcileTFOperator) handleConfigOperator() (bool, error) {37 // Define a new CR for Config Operator object38 crConfig := newCRForConfig(r.instance, r.defaults)39 // Set TFOperator instance as the owner and controller40 if err := controllerutil.SetControllerReference(r.instance, crConfig, r.scheme); err != nil {41 return false, err42 }43 // Check if this CR for Config Operator already exists44 foundCRConfig := &configv1alpha1.TFConfig{}45 err := r.client.Get(context.TODO(), types.NamespacedName{Name: crConfig.Name, Namespace: crConfig.Namespace}, foundCRConfig)46 if err != nil && errors.IsNotFound(err) {47 r.reqLogger.Info("Creating a new CR for Config Operator", "Deploy.Namespace", crConfig.Namespace, "Deploy.Name", crConfig.Name)48 err = r.client.Create(context.TODO(), crConfig)49 if err != nil {50 return false, err51 }52 // CR has been created successfully - don't requeue53 return false, nil54 } else if err != nil {55 return false, err56 } else {57 // Update envs if needed58 if r.updateSpec(&crConfig.Spec, &foundCRConfig.Spec) {59 r.reqLogger.Info(fmt.Sprintf("Updating CR for TFConfig"))60 err = r.client.Update(context.TODO(), foundCRConfig)61 if err != nil {62 r.reqLogger.Error(err, fmt.Sprintf("Cannot update CR for TFConfig"))63 return false, err64 }65 }66 }67 // CR for Config Operator already exists - don't requeue68 r.reqLogger.Info("CR for Config Operator already exists and looks updated", "Deploy.Name", foundCRConfig.Name)69 return false, nil70}71// Control Operator handler72// return true/false(Requeue), error73func (r *ReconcileTFOperator) handleControlOperator() (bool, error) {74 // Define a new CR for Control Operator object75 crControl := newCRForControl(r.instance, r.defaults)76 // Set TFOperator instance as the owner and controller77 if err := controllerutil.SetControllerReference(r.instance, crControl, r.scheme); err != nil {78 return false, err79 }80 // Check if this CR for Control Operator already exists81 foundCRControl := &controlv1alpha1.TFControl{}82 err := r.client.Get(context.TODO(), types.NamespacedName{Name: crControl.Name, Namespace: crControl.Namespace}, foundCRControl)83 if err != nil && errors.IsNotFound(err) {84 r.reqLogger.Info("Creating a new CR for Control Operator", "Deploy.Namespace", crControl.Namespace, "Deploy.Name", crControl.Name)85 err = r.client.Create(context.TODO(), crControl)86 if err != nil {87 return false, err88 }89 // CR has been created successfully - don't requeue90 return false, nil91 } else if err != nil {92 return false, err93 }94 // CR for Control Operator already exists - don't requeue95 r.reqLogger.Info("Skip reconcile: CR for Control Operator already exists", "Deploy.Namespace", foundCRControl.Namespace, "Deploy.Name", foundCRControl.Name)96 return false, nil97}98// Analytics Operator handler99// return true/false(Requeue), error100func (r *ReconcileTFOperator) handleAnalyticsOperator() (bool, error) {101 // Define a new CR for Analytics Operator object102 crAnalytics := newCRForAnalytics(r.instance, r.defaults)103 // Set TFOperator instance as the owner and controller104 if err := controllerutil.SetControllerReference(r.instance, crAnalytics, r.scheme); err != nil {105 return false, err106 }107 // Check if this CR for Analytics Operator already exists108 foundCRAnalytics := &analyticsv1alpha1.TFAnalytics{}109 err := r.client.Get(context.TODO(), types.NamespacedName{Name: crAnalytics.Name, Namespace: crAnalytics.Namespace}, foundCRAnalytics)110 if err != nil && errors.IsNotFound(err) {111 r.reqLogger.Info("Creating a new CR for Analytics Operator", "Deploy.Namespace", crAnalytics.Namespace, "Deploy.Name", crAnalytics.Name)112 err = r.client.Create(context.TODO(), crAnalytics)113 if err != nil {114 return false, err115 }116 // CR has been created successfully - don't requeue117 return false, nil118 } else if err != nil {119 return false, err120 }121 // CR for Analytics Operator already exists - don't requeue122 r.reqLogger.Info("Skip reconcile: CR for Analytics Operator already exists", "Deploy.Namespace", foundCRAnalytics.Namespace, "Deploy.Name", foundCRAnalytics.Name)123 return false, nil124}125// CfgMapHandler is structure of handlers function126type CfgMapHandler struct {127 Name string128 CfgMap *corev1.ConfigMap129}130func (r *ReconcileTFOperator) updateConfigMaps(newcm *corev1.ConfigMap, curcm *corev1.ConfigMap) bool {131 if !reflect.DeepEqual(newcm.Data, curcm.Data) {132 curcm.Data = newcm.Data133 return true134 }135 return false136}137// ConfigMaps handler, prepare global configmaps for TF138// return true/false(Requeue), error139func (r *ReconcileTFOperator) handleConfigMaps() (bool, error) {140 var handlerList []CfgMapHandler141 handlerList = append(handlerList, CfgMapHandler{Name: "tf-zookeeper-cfgmap", CfgMap: getConfigMapForZookeeper(r.instance, r.defaults)})142 handlerList = append(handlerList, CfgMapHandler{Name: "tf-rabbitmq-cfgmap", CfgMap: getConfigMapForRabbitMQ(r.instance, r.defaults)})143 handlerList = append(handlerList, CfgMapHandler{Name: "tf-cassandra-cfgmap", CfgMap: getConfigMapForCassandra(r.instance, r.defaults)})144 for _, hdl := range handlerList {145 r.reqLogger.Info("Checking: ", hdl.Name, "cfgmap")146 if err := controllerutil.SetControllerReference(r.instance, hdl.CfgMap, r.scheme); err != nil {147 return false, err148 }149 // Check if this Config CRD already exists150 foundConfigMap := &corev1.ConfigMap{}151 err := r.client.Get(context.TODO(), types.NamespacedName{Name: hdl.CfgMap.Name, Namespace: hdl.CfgMap.Namespace}, foundConfigMap)152 if err != nil && errors.IsNotFound(err) {153 r.reqLogger.Info(fmt.Sprintf("Creating a new Config Map for %s Name %s", hdl.Name, hdl.CfgMap.Name))154 err = r.client.Create(context.TODO(), hdl.CfgMap)155 if err != nil {156 return false, err157 }158 // ConfigMap has been created successfully - don't requeue159 } else if err != nil {160 return false, err161 } else {162 // ConfigMap already exists - check if is updated163 r.reqLogger.Info(fmt.Sprintf("Skip reconcile: ConfigMap for %s already exists Name %s", hdl.Name, foundConfigMap.Name))164 if r.updateConfigMaps(hdl.CfgMap, foundConfigMap) {165 r.reqLogger.Info(fmt.Sprintf("Updating ConfigMap %s", hdl.Name))166 err = r.client.Update(context.TODO(), foundConfigMap)167 if err != nil {168 r.reqLogger.Error(err, fmt.Sprintf("Cannot update ConfigMap %s", hdl.Name))169 return false, err170 }171 }172 }173 }174 return false, nil175}...

Full Screen

Full Screen

redact.go

Source:redact.go Github

copy

Full Screen

1package redact2import (3 "os"4 "github.com/pkg/errors"5 "github.com/replicatedhq/kots/pkg/util"6 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"7 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"8 v1 "k8s.io/api/core/v1"9 kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"11 "k8s.io/client-go/kubernetes"12 "sigs.k8s.io/controller-runtime/pkg/client/config"13)14func init() {15 scheme.AddToScheme(scheme.Scheme)16}17// GetRedactSpec returns the redaction yaml spec, a pretty error string, and the underlying error18func GetRedactSpec() (string, string, error) {19 cfg, err := config.GetConfig()20 if err != nil {21 return "", "failed to get cluster config", errors.Wrap(err, "failed to get cluster config")22 }23 clientset, err := kubernetes.NewForConfig(cfg)24 if err != nil {25 return "", "failed to create kubernetes clientset", errors.Wrap(err, "failed to create kubernetes clientset")26 }27 configMap, err := clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Get("kotsadm-redact", metav1.GetOptions{})28 if err != nil {29 if !kuberneteserrors.IsNotFound(err) {30 // not a not found error, so a real error31 return "", "failed to get kotsadm-redact configMap", errors.Wrap(err, "failed to get kotsadm-redact configMap")32 } else {33 // not found, so return empty string34 return "", "", nil35 }36 }37 encodedData, ok := configMap.Data["kotsadm-redact"]38 if !ok {39 return "", "failed to read kotadm-redact key in configmap", errors.New("failed to read kotadm-redact key in configmap")40 }41 return encodedData, "", nil42}43func GetRedact() (*v1beta1.Redactor, error) {44 spec, _, err := GetRedactSpec()45 if err != nil {46 return nil, err47 }48 if spec == "" {49 return nil, nil50 }51 decode := scheme.Codecs.UniversalDeserializer().Decode52 obj, _, err := decode([]byte(spec), nil, nil)53 if err != nil {54 return nil, errors.Wrap(err, "deserialize redact spec")55 }56 redactor, ok := obj.(*v1beta1.Redactor)57 if !ok {58 return nil, nil59 }60 return redactor, nil61}62// CleanupSpec attempts to parse the provided spec as a redactor, and then renders it again to clean things63func CleanupSpec(spec string) (string, error) {64 decode := scheme.Codecs.UniversalDeserializer().Decode65 obj, _, err := decode([]byte(spec), nil, nil)66 if err != nil {67 return "", errors.Wrap(err, "deserialize redact spec")68 }69 redactor, ok := obj.(*v1beta1.Redactor)70 if !ok {71 return "", errors.New("not a redact spec")72 }73 newSpec, err := util.MarshalIndent(2, redactor)74 if err != nil {75 return "", errors.Wrap(err, "marshal redact spec")76 }77 return string(newSpec), nil78}79// SetRedactSpec sets the global redact spec to the specified string, and returns a pretty error string + the underlying error80func SetRedactSpec(spec string) (string, error) {81 cfg, err := config.GetConfig()82 if err != nil {83 return "failed to get cluster config", errors.Wrap(err, "failed to get cluster config")84 }85 clientset, err := kubernetes.NewForConfig(cfg)86 if err != nil {87 return "failed to create kubernetes clientset", errors.Wrap(err, "failed to create kubernetes clientset")88 }89 configMap, err := clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Get("kotsadm-redact", metav1.GetOptions{})90 if err != nil {91 if !kuberneteserrors.IsNotFound(err) {92 // not a not found error, so a real error93 return "failed to get kotsadm-redact configMap", errors.Wrap(err, "failed to get kotsadm-redact configMap")94 } else {95 // not found, so create it fresh96 newMap := v1.ConfigMap{97 TypeMeta: metav1.TypeMeta{98 Kind: "ConfigMap",99 APIVersion: "v1",100 },101 ObjectMeta: metav1.ObjectMeta{102 Name: "kotsadm-redact",103 Namespace: os.Getenv("POD_NAMESPACE"),104 Labels: map[string]string{105 "kots.io/kotsadm": "true",106 },107 },108 Data: map[string]string{109 "kotsadm-redact": spec,110 },111 }112 _, err = clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Create(&newMap)113 if err != nil {114 return "failed to create kotsadm-redact configMap", errors.Wrap(err, "failed to create kotsadm-redact configMap")115 }116 return "", nil117 }118 }119 configMap.Data["kotsadm-redact"] = spec120 _, err = clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Update(configMap)121 if err != nil {122 return "failed to update kotsadm-redact configMap", errors.Wrap(err, "failed to update kotsadm-redact configMap")123 }124 return "", nil125}...

Full Screen

Full Screen

client.go

Source:client.go Github

copy

Full Screen

...30// Create is a method to create new configmap31func (c *Client) Create(id string, stringData map[string]string) error {32 configMapsClient := c.ClientSet.CoreV1().ConfigMaps(c.Namespace)33 ctx := context.Background()34 configMapSpec := NewSpec(id, c.Namespace, stringData)35 if _, err := configMapsClient.Create(ctx, configMapSpec, metav1.CreateOptions{}); err != nil {36 return err37 }38 return nil39}40// Get is a method to retrieve an existing configmap41func (c *Client) Get(id string) (map[string]string, error) {42 configMapsClient := c.ClientSet.CoreV1().ConfigMaps(c.Namespace)43 ctx := context.Background()44 configMapSpec, err := configMapsClient.Get(ctx, id, metav1.GetOptions{})45 if err != nil {46 return nil, err47 }48 stringData := map[string]string{}49 for key, value := range configMapSpec.Data {50 stringData[key] = value51 }52 return stringData, nil53}54// Update is a method to update an existing configmap55func (c *Client) Update(id string, stringData map[string]string) error {56 configMapsClient := c.ClientSet.CoreV1().ConfigMaps(c.Namespace)57 ctx := context.Background()58 configMapSpec := NewSpec(id, c.Namespace, stringData)59 if _, err := configMapsClient.Update(ctx, configMapSpec, metav1.UpdateOptions{}); err != nil {60 return err61 }62 return nil63}64// Apply is a method to create or update a configmap65func (c *Client) Apply(id string, stringData map[string]string) error {66 configMapsClient := c.ClientSet.CoreV1().ConfigMaps(c.Namespace)67 ctx := context.Background()68 configMapSpec := NewApplySpec(id, c.Namespace, stringData)69 if _, err := configMapsClient.Apply(ctx, configMapSpec, metav1.ApplyOptions{70 FieldManager: "application/apply-patch"}); err != nil {71 return err72 }73 return nil74}75// NewSpec is a method to return configmap spec76func NewSpec(id, namespace string, stringData map[string]string) *v1.ConfigMap {77 configuration := &v1.ConfigMap{78 ObjectMeta: metav1.ObjectMeta{79 Name: id,80 Namespace: namespace,81 },82 Data: stringData,83 }84 return configuration85}86// NewApplySpec is a method to return configmap apply spec87func NewApplySpec(id, namespace string, stringData map[string]string) *corev1.ConfigMapApplyConfiguration {88 configuration := corev1.ConfigMap(id, namespace).89 WithData(stringData)90 return configuration...

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if home := homedir.HomeDir(); home != "" {4 kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")5 } else {6 kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")7 }8 flag.Parse()9 config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)10 if err != nil {11 panic(err.Error())12 }13 clientset, err := kubernetes.NewForConfig(config)14 if err != nil {15 panic(err.Error())16 }17 retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {18 result, getErr := clientset.CoreV1().ConfigMaps("default").Get("my-config", metav1.GetOptions{})19 if getErr != nil {20 panic(fmt.Errorf("Failed to get latest version of ConfigMap: %v", getErr))21 }22 _, updateErr := clientset.CoreV1().ConfigMaps("default").Update(result)23 })24 if retryErr != nil {25 panic(fmt.Errorf("Update failed: %v", retryErr))26 }27}28import (29func main() {

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if home := homedir.HomeDir(); home != "" {4 kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")5 } else {6 kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")7 }8 flag.Parse()9 config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)10 if err != nil {11 panic(err.Error())12 }13 clientset, err := kubernetes.NewForConfig(config)14 if err != nil {15 panic(err.Error())16 }17 cm := &v1.ConfigMap{18 ObjectMeta: metav1.ObjectMeta{19 },20 Data: map[string]string{21 },22 }23 fmt.Println("Creating configmap...")24 result, err := clientset.CoreV1().ConfigMaps("default").Create(cm)25 if err != nil {26 panic(err)27 }28 fmt.Printf("Created configmap %q.\n", result.GetObjectMeta().GetName())29}

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1func NewSpec() *ConfigMapSpec {2 return &ConfigMapSpec{}3}4func NewSpec() *ConfigMapSpec {5 return &ConfigMapSpec{}6}7func NewSpec() *ConfigMapSpec {8 return &ConfigMapSpec{}9}10func NewSpec() *ConfigMapSpec {11 return &ConfigMapSpec{}12}13func NewSpec() *ConfigMapSpec {14 return &ConfigMapSpec{}15}16func NewSpec() *ConfigMapSpec {17 return &ConfigMapSpec{}18}19func NewSpec() *ConfigMapSpec {20 return &ConfigMapSpec{}21}22func NewSpec() *ConfigMapSpec {23 return &ConfigMapSpec{}24}25func NewSpec() *ConfigMapSpec {26 return &ConfigMapSpec{}27}28func NewSpec() *ConfigMapSpec {29 return &ConfigMapSpec{}30}31func NewSpec() *ConfigMapSpec {32 return &ConfigMapSpec{}33}34func NewSpec() *ConfigMapSpec {35 return &ConfigMapSpec{}36}37func NewSpec() *ConfigMapSpec {38 return &ConfigMapSpec{}39}40func NewSpec() *ConfigMapSpec {41 return &ConfigMapSpec{}42}43func NewSpec() *ConfigMapSpec

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1import (2type Specification struct {3}4func main() {5 err := envconfig.Process("", &s)6 if err != nil {7 panic(err.Error())8 }9 config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)10 if err != nil {11 panic(err.Error())12 }13 client, err := versioned.NewForConfig(config)14 if err != nil {15 panic(err.Error())16 }

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1configmap := configmap.NewSpec("my-configmap", "default")2configmap.AddData("key1", "value1")3configmap.AddData("key2", "value2")4configmap.AddData("key3", "value3")5configmap.AddData("key4", "value4")6configmap.AddData("key5", "value5")7deployment := deployment.NewSpec("my-deployment", "default")8deployment.AddLabel("app", "my-app")9deployment.AddLabel("env", "dev")10deployment.AddLabel("version", "1.0")11deployment.AddAnnotation("annotation1", "value1")12deployment.AddAnnotation("annotation2", "value2")13deployment.AddAnnotation("annotation3", "value3")14deployment.AddAnnotation("annotation4", "value4")15deployment.AddAnnotation("annotation5", "value5")16container := container.NewSpec("my-container", "nginx:1.7.9")17container.AddPort(80, 80)18container.AddPort(443, 443)19container.AddEnv("env1", "value1")20container.AddEnv("env2", "value2")21container.AddEnv("env3", "value3")22container.AddEnv("env4", "value4")23container.AddEnv("env5", "value5")24service := service.NewSpec("my-service", "default")25service.AddLabel("app", "my-app")26service.AddLabel("env", "dev")27service.AddLabel("version", "1.0")28service.AddAnnotation("annotation1", "value1")29service.AddAnnotation("annotation2", "value2")30service.AddAnnotation("annotation3", "value3")31service.AddAnnotation("annotation4", "value4")32service.AddAnnotation("annotation5", "value5")33service.AddPort(80, 80, 80)34service.AddPort(443, 443, 443)35ingress := ingress.NewSpec("my-ingress", "default")36ingress.AddLabel("app", "my-app")37ingress.AddLabel("env", "dev")38ingress.AddLabel("version", "1.0")39ingress.AddAnnotation("annotation1", "value1")40ingress.AddAnnotation("annotation2", "value

Full Screen

Full Screen

NewSpec

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 configMap := v1.ConfigMapSpec{4 Source: v1.ConfigMapNodeConfigSource{5 ConfigMap: v1.ConfigMapNameReference{6 },7 },8 }9 newConfigMap := v1.NewConfigMap(configMap)10 fmt.Println(newConfigMap)11}12&{map[configmap:{configmap}]}13import (14func main() {15 configMap := v1.ConfigMapNodeConfigSource{16 ConfigMap: v1.ConfigMapNameReference{17 },18 }19 newConfigMap := v1.NewConfigMapNode(configMap)20 fmt.Println(newConfigMap)21}22&{map[configmap:{configmap}]}23import (24func main() {25 configMap := v1.ConfigMapNameReference{26 }27 newConfigMap := v1.NewConfigMapName(configMap)28 fmt.Println(newConfigMap)29}30&{configmap}31import (32func main() {33 configMap := v1.ConfigMapVolumeSource{34 LocalObjectReference: v1.LocalObjectReference{35 },36 }37 newConfigMap := v1.NewConfigMapVolume(configMap)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful