Skip to content

Commit

Permalink
Makes standalone init env requirements injection specific (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
0sewa0 authored Jul 5, 2022
1 parent 3b69297 commit f1ee562
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions src/standalone/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type environment struct {
func newEnv() (*environment, error) {
log.Info("checking envvars")
env := &environment{}
env.setMutationTypeFields()
err := env.setRequiredFields()
if err != nil {
return nil, err
Expand All @@ -52,21 +53,18 @@ func newEnv() (*environment, error) {

func (env *environment) setRequiredFields() error {
errs := []error{}
fieldSetters := []func() error{
env.addMode,
requiredFieldSetters := []func() error{
env.addCanFail,
env.addInstallerTech,
env.addInstallPath,
env.addContainers,
env.addK8NodeName,
env.addK8PodName,
env.addK8PodUID,
env.addK8BasePodName,
env.addK8Namespace,
env.addOneAgentInjected,
env.addDataIngestInjected,
}
for _, setField := range fieldSetters {
if env.OneAgentInjected {
requiredFieldSetters = append(requiredFieldSetters, env.getOneAgentFieldSetters()...)
}

if env.DataIngestInjected {
requiredFieldSetters = append(requiredFieldSetters, env.getDataIngestFieldSetters()...)
}

for _, setField := range requiredFieldSetters {
if err := setField(); err != nil {
errs = append(errs, err)
log.Info(err.Error())
Expand All @@ -79,12 +77,36 @@ func (env *environment) setRequiredFields() error {
}

func (env *environment) setOptionalFields() {
env.addWorkloadKind()
env.addWorkloadName()
env.addInstallerUrl()
env.addInstallerFlavor()
}

func (env *environment) setMutationTypeFields() {
env.addOneAgentInjected()
env.addDataIngestInjected()
}

func (env *environment) getOneAgentFieldSetters() []func() error {
return []func() error{
env.addMode,
env.addInstallerTech,
env.addInstallPath,
env.addContainers,
env.addK8NodeName,
env.addK8PodName,
env.addK8PodUID,
env.addK8BasePodName,
env.addK8Namespace,
}
}

func (env *environment) getDataIngestFieldSetters() []func() error {
return []func() error{
env.addWorkloadKind,
env.addWorkloadName,
}
}

func (env *environment) addMode() error {
mode, err := checkEnvVar(ModeEnv)
if err != nil {
Expand Down Expand Up @@ -144,7 +166,7 @@ func (env *environment) addContainers() error {
nameEnv := fmt.Sprintf(ContainerNameEnvTemplate, i)
imageEnv := fmt.Sprintf(ContainerImageEnvTemplate, i)

containeName, err := checkEnvVar(nameEnv)
containerName, err := checkEnvVar(nameEnv)
if err != nil {
return err
}
Expand All @@ -153,7 +175,7 @@ func (env *environment) addContainers() error {
return err
}
containers = append(containers, containerInfo{
Name: containeName,
Name: containerName,
Image: imageName,
})
}
Expand Down Expand Up @@ -206,37 +228,37 @@ func (env *environment) addK8Namespace() error {
return nil
}

func (env *environment) addWorkloadKind() {
workloadKind, _ := checkEnvVar(WorkloadKindEnv)
func (env *environment) addWorkloadKind() error {
workloadKind, err := checkEnvVar(WorkloadKindEnv)
if err != nil {
return err
}
env.WorkloadKind = workloadKind
return nil
}

func (env *environment) addWorkloadName() {
workloadName, _ := checkEnvVar(WorkloadNameEnv)
func (env *environment) addWorkloadName() error {
workloadName, err := checkEnvVar(WorkloadNameEnv)
if err != nil {
return err
}
env.WorkloadName = workloadName
return nil
}

func (env *environment) addInstallerUrl() {
url, _ := checkEnvVar(InstallerUrlEnv)
env.InstallerUrl = url
}

func (env *environment) addOneAgentInjected() error {
oneAgentInjected, err := checkEnvVar(OneAgentInjectedEnv)
if err != nil {
return err
}
func (env *environment) addOneAgentInjected() {
oneAgentInjected, _ := checkEnvVar(OneAgentInjectedEnv)
env.OneAgentInjected = oneAgentInjected == "true"
return nil
}

func (env *environment) addDataIngestInjected() error {
dataIngestInjected, err := checkEnvVar(DataIngestInjectedEnv)
if err != nil {
return err
}
func (env *environment) addDataIngestInjected() {
dataIngestInjected, _ := checkEnvVar(DataIngestInjectedEnv)
env.DataIngestInjected = dataIngestInjected == "true"
return nil
}

func checkEnvVar(envvar string) (string, error) {
Expand Down

0 comments on commit f1ee562

Please sign in to comment.