18596: Warn about PreemptiblePriceFactor / InstanceTypes collision.
authorTom Clegg <tom@curii.com>
Tue, 22 Mar 2022 19:24:10 +0000 (15:24 -0400)
committerTom Clegg <tom@curii.com>
Tue, 22 Mar 2022 19:24:10 +0000 (15:24 -0400)
In case of a collision, use the explicit type, not the automatic one.

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/config/load.go
lib/config/load_test.go

index f5d42c491629fa1943ce56cbf6969e6551de4d0f..de43b9d2e2749b56ef37a100f57e57cd071d59e0 100644 (file)
@@ -287,7 +287,7 @@ func (ldr *Loader) Load() (*arvados.Config, error) {
 
        // Preprocess/automate some configs
        for id, cc := range cfg.Clusters {
-               ldr.autofillPreemptible(&cc)
+               ldr.autofillPreemptible("Clusters."+id, &cc)
 
                if strings.Count(cc.Users.AnonymousUserToken, "/") == 3 {
                        // V2 token, strip it to just a secret
@@ -536,13 +536,17 @@ func (ldr *Loader) logExtraKeys(expected, supplied map[string]interface{}, prefi
        }
 }
 
-func (ldr *Loader) autofillPreemptible(cc *arvados.Cluster) {
+func (ldr *Loader) autofillPreemptible(label string, cc *arvados.Cluster) {
        if factor := cc.Containers.PreemptiblePriceFactor; factor > 0 {
                for name, it := range cc.InstanceTypes {
                        if !it.Preemptible {
                                it.Preemptible = true
                                it.Price = it.Price * factor
                                it.Name = name + ".preemptible"
+                               if it2, exists := cc.InstanceTypes[it.Name]; exists && it2 != it {
+                                       ldr.Logger.Warnf("%s.InstanceTypes[%s]: already exists, so not automatically adding a preemptible variant of %s", label, it.Name, name)
+                                       continue
+                               }
                                cc.InstanceTypes[it.Name] = it
                        }
                }
index 3d8045d315a348a91e189dfa3e4052ca714e5e55..5270dcccce8b9d95b5b5fdb1e6fe9ad15f2e0426 100644 (file)
@@ -305,8 +305,6 @@ func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
 
 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
        var logbuf bytes.Buffer
-       logger := logrus.New()
-       logger.Out = &logbuf
        cfg, err := testLoader(c, `
 Clusters:
  zzzzz:
@@ -713,8 +711,33 @@ Clusters:
     RAM: 12345M
     VCPUs: 8
     Price: 1.23
+ z3333:
+  Containers:
+   PreemptiblePriceFactor: 0.5
+  InstanceTypes:
+   Type1:
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+   Type1.preemptible: # higher price than the auto-added variant would use -- should generate warning
+    ProviderType: Type1
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+    Preemptible: true
+   Type2:
+    RAM: 23456M
+    VCPUs: 16
+    Price: 2.46
+   Type2.preemptible: # identical to the auto-added variant -- so no warning
+    ProviderType: Type2
+    RAM: 23456M
+    VCPUs: 16
+    Price: 1.23
+    Preemptible: true
 `
-       cfg, err := testLoader(c, yaml, nil).Load()
+       var logbuf bytes.Buffer
+       cfg, err := testLoader(c, yaml, &logbuf).Load()
        c.Assert(err, check.IsNil)
        cc, err := cfg.GetCluster("z1111")
        c.Assert(err, check.IsNil)
@@ -729,4 +752,13 @@ Clusters:
        c.Check(cc.InstanceTypes["Type1.preemptible"].Price, check.Equals, 1.23/2)
        c.Check(cc.InstanceTypes["Type1.preemptible"].ProviderType, check.Equals, "Type1")
        c.Check(cc.InstanceTypes, check.HasLen, 2)
+
+       cc, err = cfg.GetCluster("z3333")
+       c.Assert(err, check.IsNil)
+       // Don't overwrite the explicitly configured preemptible variant
+       c.Check(cc.InstanceTypes["Type1.preemptible"].Price, check.Equals, 1.23)
+       c.Check(cc.InstanceTypes, check.HasLen, 4)
+       c.Check(logbuf.String(), check.Matches, `(?ms).*Clusters\.z3333\.InstanceTypes\[Type1\.preemptible\]: already exists, so not automatically adding a preemptible variant of Type1.*`)
+       c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*Type2\.preemptible.*`)
+       c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*(z1111|z2222)[^\n]*InstanceTypes.*`)
 }