Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial init changes for dynamic config. #27

Merged
merged 9 commits into from
Apr 10, 2024
46 changes: 46 additions & 0 deletions cmd/update-conf.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 0 additions & 3 deletions pkg/common-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pkg
import (
goctx "context"
"os"
"strconv"
"strings"

"github.com/go-logr/logr"
Expand All @@ -27,7 +26,6 @@ type InitParams struct {
networkInfo *networkInfo
podName string
namespace string
rackID string
nodeID string
workDir string
tlsName string
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 2 additions & 7 deletions pkg/create-aerospike-conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/refresh-cmap-restart-asd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
27 changes: 27 additions & 0 deletions pkg/update_pod_status_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -597,6 +598,32 @@ func (initp *InitParams) manageVolumesAndUpdateStatus(ctx context.Context, resta
metadata.InitializedVolumes = initializedVolumes
metadata.DirtyVolumes = dirtyVolumes

data, err := os.ReadFile(aerospikeConf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change can be put in a separate function. Let's keep functions short.

if err != nil {
return err
}

if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}

pod.Annotations["aerospikeConf"] = string(data)

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 {
// Patch the resource
if err := initp.k8sClient.Update(ctx, pod); err != nil {
abhishekdwivedi3060 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

initp.logger.Info("Pod status patched successfully", "podname", initp.podName)
return nil
}); err != nil {
return err
}

initp.logger.Info("Updating pod status", "podname", initp.podName)
abhishekdwivedi3060 marked this conversation as resolved.
Show resolved Hide resolved

return initp.updateStatus(ctx, metadata)
Expand Down
Loading