Skip to content

Commit

Permalink
avoid nil pointers (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-mez committed Apr 14, 2020
1 parent d3e21a5 commit 500f53d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/apis/datadoghq/v1alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
defaultRbacCreate = true
)

var defaultImagePullPolicy v1.PullPolicy = v1.PullIfNotPresent
var defaultImagePullPolicy = v1.PullIfNotPresent

// IsDefaultedDatadogAgent used to check if an DatadogAgent was already defaulted
// returns true if yes, else false
Expand Down Expand Up @@ -109,7 +109,7 @@ func IsDefaultedDatadogAgent(ad *DatadogAgent) bool {
}

if ad.Spec.ClusterChecksRunner != nil {
if !IsDefaultedImageConfig(&ad.Spec.ClusterAgent.Image) {
if !IsDefaultedImageConfig(&ad.Spec.ClusterChecksRunner.Image) {
return false
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/datadogagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func newDaemonSetFromInstance(agentdeployment *datadoghqv1alpha1.DatadogAgent, s
}

func daemonsetName(agentdeployment *datadoghqv1alpha1.DatadogAgent) string {
if agentdeployment.Spec.Agent.DaemonsetName != "" {
if agentdeployment.Spec.Agent != nil && agentdeployment.Spec.Agent.DaemonsetName != "" {
return agentdeployment.Spec.Agent.DaemonsetName
}
return agentdeployment.Name
Expand Down Expand Up @@ -380,5 +380,8 @@ func getAgentCustomConfigConfigMapName(dda *datadoghqv1alpha1.DatadogAgent) stri
}

func buildAgentConfigurationConfigMap(dda *datadoghqv1alpha1.DatadogAgent) (*corev1.ConfigMap, error) {
if dda.Spec.Agent == nil {
return nil, nil
}
return buildConfigurationConfigMap(dda, dda.Spec.Agent.CustomConfig, getAgentCustomConfigConfigMapName(dda), datadoghqv1alpha1.AgentCustomConfigVolumeSubPath)
}
35 changes: 21 additions & 14 deletions pkg/controller/datadogagent/clusteragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,6 @@ func getEnvVarsForClusterAgent(dda *datadoghqv1alpha1.DatadogAgent) []corev1.Env
Name: datadoghqv1alpha1.DDSite,
Value: spec.Site,
},
{
Name: datadoghqv1alpha1.DDddURL,
Value: *spec.Agent.Config.DDUrl,
},
{
Name: datadoghqv1alpha1.DDClusterChecksEnabled,
Value: datadoghqv1alpha1.BoolToString(spec.ClusterAgent.Config.ClusterChecksEnabled),
Expand All @@ -440,6 +436,13 @@ func getEnvVarsForClusterAgent(dda *datadoghqv1alpha1.DatadogAgent) []corev1.Env
},
}

if spec.Agent != nil {
envVars = append(envVars, corev1.EnvVar{
Name: datadoghqv1alpha1.DDddURL,
Value: *spec.Agent.Config.DDUrl,
})
}

if spec.ClusterAgent.Config.LogLevel != nil {
envVars = append(envVars, corev1.EnvVar{
Name: datadoghqv1alpha1.DDLogLevel,
Expand Down Expand Up @@ -769,12 +772,14 @@ func buildAgentClusterRole(dda *datadoghqv1alpha1.DatadogAgent, name, version st
// to collect cluster level metrics and events
rbacRules = append(rbacRules, getDefaultClusterAgentPolicyRules()...)

if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.CollectEvents) {
rbacRules = append(rbacRules, getEventCollectionPolicyRule())
}
if dda.Spec.Agent != nil {
if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.CollectEvents) {
rbacRules = append(rbacRules, getEventCollectionPolicyRule())
}

if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.LeaderElection) {
rbacRules = append(rbacRules, getLeaderElectionPolicyRule()...)
if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.LeaderElection) {
rbacRules = append(rbacRules, getLeaderElectionPolicyRule()...)
}
}
}

Expand Down Expand Up @@ -855,12 +860,14 @@ func buildClusterAgentClusterRole(dda *datadoghqv1alpha1.DatadogAgent, name, age
Verbs: []string{datadoghqv1alpha1.ListVerb, datadoghqv1alpha1.WatchVerb},
})

if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.CollectEvents) {
rbacRules = append(rbacRules, getEventCollectionPolicyRule())
}
if dda.Spec.Agent != nil {
if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.CollectEvents) {
rbacRules = append(rbacRules, getEventCollectionPolicyRule())
}

if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.LeaderElection) {
rbacRules = append(rbacRules, getLeaderElectionPolicyRule()...)
if datadoghqv1alpha1.BoolValue(dda.Spec.Agent.Config.LeaderElection) {
rbacRules = append(rbacRules, getLeaderElectionPolicyRule()...)
}
}

if datadoghqv1alpha1.BoolValue(dda.Spec.ClusterAgent.Config.MetricsProviderEnabled) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/datadogagent/clusteragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func clusterAgentDefaultEnvVars() []corev1.EnvVar {
Name: "DD_SITE",
Value: "",
},
{
Name: "DD_DD_URL",
Value: "https://app.datadoghq.com",
},
{
Name: "DD_CLUSTER_CHECKS_ENABLED",
Value: "false",
Expand All @@ -92,6 +88,10 @@ func clusterAgentDefaultEnvVars() []corev1.EnvVar {
Name: "DD_LEADER_ELECTION",
Value: "true",
},
{
Name: "DD_DD_URL",
Value: "https://app.datadoghq.com",
},
{
Name: "DD_API_KEY",
Value: "",
Expand Down

0 comments on commit 500f53d

Please sign in to comment.