Skip to content

Commit 3b8ea12

Browse files
author
Nikolas De Giorgis
authored
add WithAgentFlags function (#148)
1 parent ab14dc5 commit 3b8ea12

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

cmd/versionhook/main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/pkg/errors"
1313

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

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

105105
health, err := getAgentHealthStatus()
106106
if err != nil {
107-
return agenthealth.Health{}, err
107+
return agent.Health{}, err
108108
}
109109

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

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

123123
}
124124

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

134134
h, err := readAgentHealthStatus(f)
135135
if err != nil {
136-
return agenthealth.Health{}, errors.Errorf("could not read health status file: %s", err)
136+
return agent.Health{}, errors.Errorf("could not read health status file: %s", err)
137137
}
138138
return h, err
139139
}
140140

141141
// readAgentHealthStatus reads an instance of health.Health from the provided
142142
// io.Reader
143-
func readAgentHealthStatus(reader io.Reader) (agenthealth.Health, error) {
144-
var h agenthealth.Health
143+
func readAgentHealthStatus(reader io.Reader) (agent.Health, error) {
144+
var h agent.Health
145145
data, err := ioutil.ReadAll(reader)
146146
if err != nil {
147147
return h, err
@@ -157,7 +157,7 @@ func getHostname() string {
157157
// shouldDeletePod returns a boolean value indicating if this pod should be deleted
158158
// this would be the case if the agent is currently trying to upgrade the version
159159
// of mongodb.
160-
func shouldDeletePod(health agenthealth.Health) (bool, error) {
160+
func shouldDeletePod(health agent.Health) (bool, error) {
161161
status, ok := health.ProcessPlans[getHostname()]
162162
if !ok {
163163
return false, errors.Errorf("hostname %s was not in the process plans", getHostname())
@@ -169,7 +169,7 @@ func shouldDeletePod(health agenthealth.Health) (bool, error) {
169169
// on the mongod pod to be restarted. In order to do this, we need to check the agent
170170
// status file and determine if the mongod has been stopped and if we are in the process
171171
// of a version change.
172-
func isWaitingToBeDeleted(healthStatus agenthealth.MmsDirectorStatus) bool {
172+
func isWaitingToBeDeleted(healthStatus agent.MmsDirectorStatus) bool {
173173
if len(healthStatus.Plans) == 0 {
174174
return false
175175
}
@@ -223,7 +223,7 @@ func getThisPod() (corev1.Pod, error) {
223223
func inClusterClient() (client.Client, error) {
224224
config, err := rest.InClusterConfig()
225225
if err != nil {
226-
return nil, errors.Errorf("could not get cluster config: %%s", err)
226+
return nil, errors.Errorf("could not get cluster config: %s", err)
227227
}
228228

229229
k8sClient, err := client.New(config, client.Options{})

pkg/agent/agentflags.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package agent
2+
3+
import corev1 "k8s.io/api/core/v1"
4+
5+
type StartupParameter struct {
6+
Key string `json:"key"`
7+
Value string `json:"value"`
8+
}
9+
10+
// StartupParametersToAgentFlag takes a slice of StartupParameters
11+
// and concatenates them into a single string that is then
12+
// returned as env variable AGENT_FLAGS
13+
func StartupParametersToAgentFlag(parameters ...StartupParameter) corev1.EnvVar {
14+
agentParams := ""
15+
for _, param := range parameters {
16+
agentParams += " -" + param.Key + " " + param.Value
17+
}
18+
return corev1.EnvVar{Name: "AGENT_FLAGS", Value: agentParams}
19+
}

pkg/agent/agentflags_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package agent
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestAgentFlagIsCorrectlyCreated(t *testing.T) {
10+
parameters := []StartupParameter{
11+
{
12+
Key: "Key1",
13+
Value: "Value1",
14+
},
15+
{
16+
Key: "Key2",
17+
Value: "Value2",
18+
},
19+
}
20+
21+
envVar := StartupParametersToAgentFlag(parameters...)
22+
assert.Equal(t, "AGENT_FLAGS", envVar.Name)
23+
assert.Equal(t, " -Key1 Value1 -Key2 Value2", envVar.Value)
24+
25+
}
26+
27+
func TestAgentFlagEmptyParameters(t *testing.T) {
28+
parameters := []StartupParameter{}
29+
30+
envVar := StartupParametersToAgentFlag(parameters...)
31+
assert.Equal(t, "AGENT_FLAGS", envVar.Name)
32+
assert.Equal(t, "", envVar.Value)
33+
34+
}

pkg/agenthealth/agenthealth.go renamed to pkg/agent/agenthealth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package agenthealth
1+
package agent
22

33
import (
44
"time"

0 commit comments

Comments
 (0)