func TestProxyDefaults(t *testing.T) { defaultsConfig := &defaultsapi.BuildDefaultsConfig{ GitHTTPProxy: "http", GitHTTPSProxy: "https", GitNoProxy: "no", } admitter := BuildDefaults{defaultsConfig} pod := u.Pod().WithBuild(t, u.Build().WithDockerStrategy().AsBuild(), "v1") err := admitter.ApplyDefaults((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } if build.Spec.Source.Git.HTTPProxy == nil || len(*build.Spec.Source.Git.HTTPProxy) == 0 || *build.Spec.Source.Git.HTTPProxy != "http" { t.Errorf("failed to find http proxy in git source") } if build.Spec.Source.Git.HTTPSProxy == nil || len(*build.Spec.Source.Git.HTTPSProxy) == 0 || *build.Spec.Source.Git.HTTPSProxy != "https" { t.Errorf("failed to find http proxy in git source") } if build.Spec.Source.Git.NoProxy == nil || len(*build.Spec.Source.Git.NoProxy) == 0 || *build.Spec.Source.Git.NoProxy != "no" { t.Errorf("failed to find no proxy setting in git source") } }
// ApplyOverrides applies configured overrides to a build in a build pod func (b BuildOverrides) ApplyOverrides(pod *kapi.Pod) error { if b.config == nil { return nil } build, version, err := buildadmission.GetBuildFromPod(pod) if err != nil { return err } glog.V(4).Infof("Applying overrides to build %s/%s", build.Namespace, build.Name) if b.config.ForcePull { if build.Spec.Strategy.DockerStrategy != nil { glog.V(5).Infof("Setting docker strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.DockerStrategy.ForcePull = true } if build.Spec.Strategy.SourceStrategy != nil { glog.V(5).Infof("Setting source strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.SourceStrategy.ForcePull = true } if build.Spec.Strategy.CustomStrategy != nil { err := applyForcePullToPod(pod) if err != nil { return err } glog.V(5).Infof("Setting custom strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.CustomStrategy.ForcePull = true } } // Apply label overrides for _, lbl := range b.config.ImageLabels { glog.V(5).Infof("Overriding image label %s=%s in build %s/%s", lbl.Name, lbl.Value, build.Namespace, build.Name) overrideLabel(lbl, &build.Spec.Output.ImageLabels) } if len(b.config.NodeSelector) != 0 && pod.Spec.NodeSelector == nil { pod.Spec.NodeSelector = map[string]string{} } for k, v := range b.config.NodeSelector { glog.V(5).Infof("Adding override nodeselector %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name) pod.Spec.NodeSelector[k] = v } if len(b.config.Annotations) != 0 && pod.Annotations == nil { pod.Annotations = map[string]string{} } for k, v := range b.config.Annotations { glog.V(5).Infof("Adding override annotation %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name) pod.Annotations[k] = v } return buildadmission.SetBuildInPod(pod, build, version) }
func TestIncrementalDefaults(t *testing.T) { bool_t := true defaultsConfig := &defaultsapi.BuildDefaultsConfig{ SourceStrategyDefaults: &defaultsapi.SourceStrategyDefaultsConfig{ Incremental: &bool_t, }, } admitter := BuildDefaults{defaultsConfig} pod := u.Pod().WithBuild(t, u.Build().WithSourceStrategy().AsBuild(), "v1") err := admitter.ApplyDefaults((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } if !*build.Spec.Strategy.SourceStrategy.Incremental { t.Errorf("failed to default incremental to true") } build = u.Build().WithSourceStrategy().AsBuild() bool_f := false build.Spec.Strategy.SourceStrategy.Incremental = &bool_f pod = u.Pod().WithBuild(t, build, "v1") err = admitter.ApplyDefaults((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } build, _, err = buildadmission.GetBuildFromPod((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } if *build.Spec.Strategy.SourceStrategy.Incremental { t.Errorf("should not have overridden incremental to true") } }
func TestEnvDefaults(t *testing.T) { defaultsConfig := &defaultsapi.BuildDefaultsConfig{ Env: []kapi.EnvVar{ { Name: "VAR1", Value: "VALUE1", }, { Name: "VAR2", Value: "VALUE2", }, }, } admitter := BuildDefaults{defaultsConfig} pod := u.Pod().WithBuild(t, u.Build().WithSourceStrategy().AsBuild(), "v1") err := admitter.ApplyDefaults((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod)) if err != nil { t.Fatalf("unexpected error: %v", err) } env := getBuildEnv(build) var1found, var2found := false, false for _, ev := range *env { if ev.Name == "VAR1" { if ev.Value != "VALUE1" { t.Errorf("unexpected value %s", ev.Value) } var1found = true } if ev.Name == "VAR2" { if ev.Value != "VALUE2" { t.Errorf("unexpected value %s", ev.Value) } var2found = true } } if !var1found { t.Errorf("VAR1 not found") } if !var2found { t.Errorf("VAR2 not found") } }
// ApplyDefaults applies configured build defaults to a build pod func (b BuildDefaults) ApplyDefaults(pod *kapi.Pod) error { if b.config == nil { return nil } build, version, err := buildadmission.GetBuildFromPod(pod) if err != nil { return nil } glog.V(4).Infof("Applying defaults to build %s/%s", build.Namespace, build.Name) b.applyBuildDefaults(build) b.applyPodDefaults(pod) err = buildadmission.SetPodLogLevelFromBuild(pod, build) if err != nil { return err } return buildadmission.SetBuildInPod(pod, build, version) }
func TestResourceDefaults(t *testing.T) { tests := map[string]struct { DefaultResource kapi.ResourceRequirements BuildResource kapi.ResourceRequirements ExpectedResource kapi.ResourceRequirements }{ "BuildDefaults plugin and Build object both defined resource limits and requests": { DefaultResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, BuildResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("40"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("4G"), }, }, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("40"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("4G"), }, }, }, "BuildDefaults plugin defined limits and requests, Build object defined resource requests": { DefaultResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, BuildResource: kapi.ResourceRequirements{ Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, }, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, }, }, "BuildDefaults plugin defined limits and requests, Build object defined resource limits": { DefaultResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, BuildResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, }, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), }, }, }, "BuildDefaults plugin defined nothing, Build object defined resource limits": { DefaultResource: kapi.ResourceRequirements{}, BuildResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, }, "BuildDefaults plugin and Build object defined nothing": { DefaultResource: kapi.ResourceRequirements{}, BuildResource: kapi.ResourceRequirements{}, ExpectedResource: kapi.ResourceRequirements{}, }, "BuildDefaults plugin defined limits and requests, Build object defined nothing": { DefaultResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, BuildResource: kapi.ResourceRequirements{}, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, }, "BuildDefaults plugin defined part of limits and requests, Build object defined part of limits and requests": { DefaultResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, BuildResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), }, }, ExpectedResource: kapi.ResourceRequirements{ Limits: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"), }, Requests: kapi.ResourceList{ kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"), kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"), }, }, }, } for name, test := range tests { defaults := BuildDefaults{config: &defaultsapi.BuildDefaultsConfig{Resources: test.DefaultResource}} build := u.Build().WithSourceStrategy().AsBuild() build.Spec.Resources = test.BuildResource pod := u.Pod().WithBuild(t, build, "v1") err := defaults.ApplyDefaults((*kapi.Pod)(pod)) if err != nil { t.Fatalf("%v :unexpected error: %v", name, err) } build, _, err = buildadmission.GetBuildFromPod((*kapi.Pod)(pod)) if err != nil { t.Fatalf("%v :unexpected error: %v", name, err) } if !kapi.Semantic.DeepEqual(test.ExpectedResource, build.Spec.Resources) { t.Fatalf("%v:Expected expected=actual, %v != %v", name, test.ExpectedResource, build.Spec.Resources) } } }