diff --git a/cmd/update-conf.go b/cmd/update-conf.go new file mode 100644 index 0000000..0fc0e60 --- /dev/null +++ b/cmd/update-conf.go @@ -0,0 +1,46 @@ +/* +Copyright 2023 The aerospike-operator Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +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 cmd + +import ( + goctx "context" + + "github.com/spf13/cobra" + + "github.com/aerospike/aerospike-kubernetes-init/pkg" +) + +// confUpdate represents the update-conf command +var confUpdate = &cobra.Command{ + Use: "update-conf", + Short: "update conf init functionality", + Long: `This command updates conf file of aerospike server and CR status.`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := goctx.TODO() + + initParams, err := pkg.PopulateInitParams(ctx) + if err != nil { + return err + } + return initParams.UpdateConf(ctx, cmName, cmNamespace) + }, +} + +func init() { + rootCmd.AddCommand(confUpdate) + confUpdate.Flags().StringVar(&cmName, "cm-name", "", "configmap name") + confUpdate.Flags().StringVar(&cmNamespace, "cm-namespace", "", "configmap namespace") +} diff --git a/go.mod b/go.mod index 7411b8c..3c58550 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.21.8 require ( - github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240318075533-572f8bf903d8 + github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240322083457-4f28102c47fa github.com/go-logr/logr v1.3.0 github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 github.com/mitchellh/go-ps v1.0.0 @@ -19,7 +19,7 @@ require ( require ( github.com/aerospike/aerospike-client-go/v6 v6.14.0 // indirect - github.com/aerospike/aerospike-management-lib v1.2.1 // indirect + github.com/aerospike/aerospike-management-lib v1.2.1-0.20240217101322-63da5d036614 // indirect github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect @@ -56,7 +56,6 @@ require ( github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.10.1 // indirect - github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect diff --git a/go.sum b/go.sum index 9edadc9..d4a9270 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ github.com/aerospike/aerospike-client-go/v6 v6.14.0 h1:Z3FcGWJda1sagzdc6Akz4EJ13Pq55Uyn6qtFLrVUDd0= github.com/aerospike/aerospike-client-go/v6 v6.14.0/go.mod h1:/0Wm81GhMqem+9flWcpazPKoRfjFeG6WrQdXGiMNi0A= -github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240318075533-572f8bf903d8 h1:CquYTj1rUPjwvxxnW8si1oHy2dqBdxgZmhzzWN15Xe8= -github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240318075533-572f8bf903d8/go.mod h1:dqpRPS6RFuRCFc8thmc3UMmxYo9UY/EfL8ZBhQw1Kfg= -github.com/aerospike/aerospike-management-lib v1.2.1 h1:VgKJzQ/zcyqGMhivvSuqmGIFtByHavtVPGX7ZUE+gQI= -github.com/aerospike/aerospike-management-lib v1.2.1/go.mod h1:EtWBVE/1TlphicTvpchAjpAgjoFXddvZAXXyrvqWKu0= +github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240322083457-4f28102c47fa h1:gK4+u0uIdiEl0wE7i0Zs3w96IAAJGxtLnTVrsujt34w= +github.com/aerospike/aerospike-kubernetes-operator v0.0.0-20240322083457-4f28102c47fa/go.mod h1:6POrATwl8q3G8In63p8ffbFhFo4IMud2Qv/lxIA09gY= +github.com/aerospike/aerospike-management-lib v1.2.1-0.20240217101322-63da5d036614 h1:tdpAQb+7k4eTNR8/UWiLna9lGVpzhmhxMKmsfYjBFhY= +github.com/aerospike/aerospike-management-lib v1.2.1-0.20240217101322-63da5d036614/go.mod h1:cSmDG+UbsNt6uTpjdHPfVYvxsHiDh0R+iA5D6vqcLeI= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ= @@ -123,10 +123,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= -github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494 h1:wSmWgpuccqS2IOfmYrbRiUgv+g37W5suLLLxwwniTSc= -github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494/go.mod h1:yipyliwI08eQ6XwDm1fEwKPdF/xdbkiHtrU+1Hg+vc4= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= @@ -136,7 +134,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -243,7 +240,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/pkg/common-env.go b/pkg/common-env.go index d8f42c8..90c6651 100644 --- a/pkg/common-env.go +++ b/pkg/common-env.go @@ -3,7 +3,6 @@ package pkg import ( goctx "context" "os" - "strconv" "strings" "github.com/go-logr/logr" @@ -27,7 +26,6 @@ type InitParams struct { networkInfo *networkInfo podName string namespace string - rackID string nodeID string workDir string tlsName string @@ -108,7 +106,6 @@ func PopulateInitParams(ctx goctx.Context) (*InitParams, error) { k8sClient: k8sClient, podName: podName, namespace: namespace, - rackID: strconv.Itoa(rack.ID), nodeID: nodeID, workDir: workDir, logger: logger, diff --git a/pkg/create-aerospike-conf.go b/pkg/create-aerospike-conf.go index 464d13a..48921e3 100644 --- a/pkg/create-aerospike-conf.go +++ b/pkg/create-aerospike-conf.go @@ -29,12 +29,7 @@ func (initp *InitParams) createAerospikeConf() error { confString := string(data) - // Update node and rack ids configuration file - re := regexp.MustCompile("rack-id.*0") - if rackStr := re.FindString(confString); rackStr != "" { - confString = strings.ReplaceAll(confString, rackStr, "rack-id "+initp.rackID) - } - + // Update node ids in configuration file confString = strings.ReplaceAll(confString, "ENV_NODE_ID", initp.nodeID) if initp.networkInfo.podPort != 0 { @@ -78,7 +73,7 @@ func (initp *InitParams) createAerospikeConf() error { fileScanner := bufio.NewScanner(readFile) - // Update mesh seeds in the configuration file + // Update mesh seeds in the configuration file for fileScanner.Scan() { peer := fileScanner.Text() if strings.Contains(peer, initp.podName) { diff --git a/pkg/refresh-cmap-restart-asd.go b/pkg/refresh-cmap-restart-asd.go index a645596..a153d58 100644 --- a/pkg/refresh-cmap-restart-asd.go +++ b/pkg/refresh-cmap-restart-asd.go @@ -26,3 +26,29 @@ func (initp *InitParams) QuickRestart(ctx goctx.Context, cmName, cmNamespace str // Update pod status in the k8s aerospike cluster object return initp.manageVolumesAndUpdateStatus(ctx, "quickRestart") } + +func (initp *InitParams) UpdateConf(ctx goctx.Context, cmName, cmNamespace string) error { + if cmNamespace == "" { + return fmt.Errorf("kubernetes namespace required as an argument") + } + + if cmName == "" { + return fmt.Errorf("aerospike configmap required as an argument") + } + + if err := initp.ExportK8sConfigmap(ctx, cmNamespace, cmName, configMapDir); err != nil { + return err + } + + // Create new Aerospike configuration + if err := initp.copyTemplates(configMapDir, configVolume); err != nil { + return err + } + + if err := initp.createAerospikeConf(); err != nil { + return err + } + + // Update pod status in the k8s aerospike cluster object + return initp.manageVolumesAndUpdateStatus(ctx, "noRestart") +} diff --git a/pkg/update_pod_status_util.go b/pkg/update_pod_status_util.go index 3738207..ee555a0 100644 --- a/pkg/update_pod_status_util.go +++ b/pkg/update_pod_status_util.go @@ -18,6 +18,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1" @@ -606,9 +607,40 @@ func (initp *InitParams) manageVolumesAndUpdateStatus(ctx context.Context, resta metadata.Image = podImage metadata.InitializedVolumes = initializedVolumes metadata.DirtyVolumes = dirtyVolumes + metadata.DynamicConfigFailed = false metadata.IsSecurityEnabled = securityEnabled - initp.logger.Info("Updating pod status", "podname", initp.podName) + data, err := os.ReadFile(aerospikeConf) + if err != nil { + return err + } + + if err := retry.OnError(retry.DefaultBackoff, func(err error) bool { + // Customize the error check for retrying, return true to retry, false to stop retrying + return true + }, func() error { + if err := initp.k8sClient.Get(ctx, podNamespacedName, pod); err != nil { + return err + } + + if pod.Annotations == nil { + pod.Annotations = make(map[string]string) + } + + pod.Annotations["aerospikeConf"] = string(data) + + // Patch the resource + if err := initp.k8sClient.Update(ctx, pod); err != nil { + return err + } + + initp.logger.Info("Patched/updated pod annotation successfully", "podname", initp.podName) + return nil + }); err != nil { + return err + } + + initp.logger.Info("Updating pod status in CR", "podname", initp.podName) return initp.updateStatus(ctx, metadata) }