Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions cmd/versionhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/pkg/errors"

"github.com/mongodb/mongodb-kubernetes-operator/pkg/agenthealth"
"github.com/mongodb/mongodb-kubernetes-operator/pkg/agent"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -91,7 +91,7 @@ func setupLogger() *zap.SugaredLogger {
// waitForAgentHealthStatus will poll the health status file and wait for it to be updated.
// The agent doesn't write the plan to the file right away and hence we need to wait for the
// latest plan to be written.
func waitForAgentHealthStatus() (agenthealth.Health, error) {
func waitForAgentHealthStatus() (agent.Health, error) {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()

Expand All @@ -104,12 +104,12 @@ func waitForAgentHealthStatus() (agenthealth.Health, error) {

health, err := getAgentHealthStatus()
if err != nil {
return agenthealth.Health{}, err
return agent.Health{}, err
}

status, ok := health.Healthiness[getHostname()]
if !ok {
return agenthealth.Health{}, errors.Errorf("couldn't find status for hostname %s", getHostname())
return agent.Health{}, errors.Errorf("couldn't find status for hostname %s", getHostname())
}

// We determine if the file has been updated by checking if the process is not in goal state.
Expand All @@ -118,30 +118,30 @@ func waitForAgentHealthStatus() (agenthealth.Health, error) {
return health, nil
}
}
return agenthealth.Health{}, errors.Errorf("agent health status not ready after waiting %s", pollingDuration.String())
return agent.Health{}, errors.Errorf("agent health status not ready after waiting %s", pollingDuration.String())

}

// getAgentHealthStatus returns an instance of agenthealth.Health read
// getAgentHealthStatus returns an instance of agent.Health read
// from the health file on disk
func getAgentHealthStatus() (agenthealth.Health, error) {
func getAgentHealthStatus() (agent.Health, error) {
f, err := os.Open(os.Getenv(agentStatusFilePathEnv))
if err != nil {
return agenthealth.Health{}, errors.Errorf("could not open file: %s", err)
return agent.Health{}, errors.Errorf("could not open file: %s", err)
}
defer f.Close()

h, err := readAgentHealthStatus(f)
if err != nil {
return agenthealth.Health{}, errors.Errorf("could not read health status file: %s", err)
return agent.Health{}, errors.Errorf("could not read health status file: %s", err)
}
return h, err
}

// readAgentHealthStatus reads an instance of health.Health from the provided
// io.Reader
func readAgentHealthStatus(reader io.Reader) (agenthealth.Health, error) {
var h agenthealth.Health
func readAgentHealthStatus(reader io.Reader) (agent.Health, error) {
var h agent.Health
data, err := ioutil.ReadAll(reader)
if err != nil {
return h, err
Expand All @@ -157,7 +157,7 @@ func getHostname() string {
// shouldDeletePod returns a boolean value indicating if this pod should be deleted
// this would be the case if the agent is currently trying to upgrade the version
// of mongodb.
func shouldDeletePod(health agenthealth.Health) (bool, error) {
func shouldDeletePod(health agent.Health) (bool, error) {
status, ok := health.ProcessPlans[getHostname()]
if !ok {
return false, errors.Errorf("hostname %s was not in the process plans", getHostname())
Expand All @@ -169,7 +169,7 @@ func shouldDeletePod(health agenthealth.Health) (bool, error) {
// on the mongod pod to be restarted. In order to do this, we need to check the agent
// status file and determine if the mongod has been stopped and if we are in the process
// of a version change.
func isWaitingToBeDeleted(healthStatus agenthealth.MmsDirectorStatus) bool {
func isWaitingToBeDeleted(healthStatus agent.MmsDirectorStatus) bool {
if len(healthStatus.Plans) == 0 {
return false
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func getThisPod() (corev1.Pod, error) {
func inClusterClient() (client.Client, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Errorf("could not get cluster config: %%s", err)
return nil, errors.Errorf("could not get cluster config: %s", err)
}

k8sClient, err := client.New(config, client.Options{})
Expand Down
19 changes: 19 additions & 0 deletions pkg/agent/agentflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package agent

import corev1 "k8s.io/api/core/v1"

type StartupParameter struct {
Key string `json:"key"`
Value string `json:"value"`
}

// StartupParametersToAgentFlag takes a slice of StartupParameters
// and concatenates them into a single string that is then
// returned as env variable AGENT_FLAGS
func StartupParametersToAgentFlag(parameters ...StartupParameter) corev1.EnvVar {
agentParams := ""
for _, param := range parameters {
agentParams += " -" + param.Key + " " + param.Value
}
return corev1.EnvVar{Name: "AGENT_FLAGS", Value: agentParams}
}
34 changes: 34 additions & 0 deletions pkg/agent/agentflags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package agent

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAgentFlagIsCorrectlyCreated(t *testing.T) {
parameters := []StartupParameter{
{
Key: "Key1",
Value: "Value1",
},
{
Key: "Key2",
Value: "Value2",
},
}

envVar := StartupParametersToAgentFlag(parameters...)
assert.Equal(t, "AGENT_FLAGS", envVar.Name)
assert.Equal(t, " -Key1 Value1 -Key2 Value2", envVar.Value)

}

func TestAgentFlagEmptyParameters(t *testing.T) {
parameters := []StartupParameter{}

envVar := StartupParametersToAgentFlag(parameters...)
assert.Equal(t, "AGENT_FLAGS", envVar.Name)
assert.Equal(t, "", envVar.Value)

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agenthealth
package agent

import (
"time"
Expand Down