Skip to content

Commit

Permalink
[dekchouse] Fix stabilize release channel feature
Browse files Browse the repository at this point in the history
(cherry picked from commit 0af4a72)
  • Loading branch information
shvgn authored and konstantin-axenov committed Jul 23, 2021
1 parent 4b2bd7b commit 34e96fb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
33 changes: 14 additions & 19 deletions modules/020-deckhouse/hooks/set_module_image_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package hooks

import (
"fmt"
"regexp"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
Expand Down Expand Up @@ -67,28 +66,24 @@ func parseDeckhouseImage(input *go_hook.HookInput) error {
if len(deckhouseSnapshot) != 1 {
return fmt.Errorf("deckhouse was not able to find an image of itself")
}

image := deckhouseSnapshot[0].(string)
input.Values.Set(deckhouseImagePath, image)

repo := input.Values.Get(repoPath).String()
repoPattern := fmt.Sprintf("^%s[:,/](.*)$", regexp.QuoteMeta(repo))
repoRegex, err := regexp.Compile(repoPattern)
if err != nil {
return fmt.Errorf("cannot complie regex %q", repoPattern)
}

captureGroups := repoRegex.FindStringSubmatch(image)

metricResult := float64(1)
if len(captureGroups) == 2 {
switch captureGroups[1] {
case "alpha", "beta", "early-access", "stable", "rock-solid":
metricResult = 0
}
// Set deckhouse image only if it was not set before, e.g. by stabilize_release_channel hook
if input.Values.Get(deckhouseImagePath).String() == "" {
input.Values.Set(deckhouseImagePath, image)
}

input.MetricsCollector.Set("d8_deckhouse_is_not_on_release_channel", metricResult, nil)
// Generate alert for deckhouse being not on release channel
repo := input.Values.Get(repoPath).String()
input.MetricsCollector.Set("d8_deckhouse_is_not_on_release_channel", isOnReleaseChannelMetricValue(image, repo), nil)

return nil
}

func isOnReleaseChannelMetricValue(image, repo string) float64 {
_, isKnown := parseReleaseChannel(image, repo)
if isKnown {
return 0
}
return 1
}
74 changes: 64 additions & 10 deletions modules/020-deckhouse/hooks/set_module_image_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ global:
modulesImages:
registry: registry.deckhouse.io
deckhouse:
internal:
currentReleaseImageName: "test"
internal: {}
`, `{}`)

Context("With Deckhouse pod", func() {
BeforeEach(func() {
f.BindingContexts.Set(f.KubeStateSetAndWaitForBindingContexts(`
Context("when image in absent values", func() {
BeforeEach(func() {
f.BindingContexts.Set(f.KubeStateSetAndWaitForBindingContexts(`
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -54,14 +54,68 @@ spec:
- name: deckhouse
image: registry.deckhouse.io/deckhouse/ce/dev:test
`, 1))
f.RunHook()
f.RunHook()
})

It("Should run", func() {
Expect(f).To(ExecuteSuccessfully())
deployment := f.KubernetesResource("Deployment", "d8-system", "deckhouse")
Expect(deployment.Exists()).To(BeTrue())
Expect(f.ValuesGet("deckhouse.internal.currentReleaseImageName").String()).To(Equal("registry.deckhouse.io/deckhouse/ce/dev:test"))
})
})

Context("when image in present values", func() {
BeforeEach(func() {
f.ValuesSet("deckhouse.internal.currentReleaseImageName", "registry.deckhouse.io/deckhouse/ce/initial:test")
f.BindingContexts.Set(f.KubeStateSetAndWaitForBindingContexts(`
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: deckhouse
heritage: deckhouse
module: deckhouse
name: deckhouse
namespace: d8-system
spec:
template:
spec:
containers:
- name: deckhouse
image: registry.deckhouse.io/deckhouse/ce/different:test
`, 1))
f.RunHook()
})

It("Should run", func() {
Expect(f).To(ExecuteSuccessfully())
deployment := f.KubernetesResource("Deployment", "d8-system", "deckhouse")
Expect(deployment.Exists()).To(BeTrue())
Expect(f.ValuesGet("deckhouse.internal.currentReleaseImageName").String()).To(Equal("registry.deckhouse.io/deckhouse/ce/initial:test"))
})
})
})

Context("isOnReleaseChannelMetricValue func", func() {
It("returns 0 when image is on a release channel", func() {
var (
repo = "registry.deckhouse.io/deckhouse/fe"
image = "registry.deckhouse.io/deckhouse/fe:early-access"
)
metricValue := isOnReleaseChannelMetricValue(image, repo)
Expect(metricValue).To(Equal(float64(0)))
})

It("Should run", func() {
Expect(f).To(ExecuteSuccessfully())
deployment := f.KubernetesResource("Deployment", "d8-system", "deckhouse")
Expect(deployment.Exists()).To(BeTrue())
Expect(f.ValuesGet("deckhouse.internal.currentReleaseImageName").String()).To(Equal("registry.deckhouse.io/deckhouse/ce/dev:test"))
It("returns 1 when image is NOT on a release channel", func() {
var (
repo = "registry.deckhouse.io/deckhouse/fe"
image = "registry.deckhouse.io/deckhouse/fe:late-access"
)
metricValue := isOnReleaseChannelMetricValue(image, repo)

Expect(metricValue).To(Equal(float64(1)))
})
})
})
3 changes: 0 additions & 3 deletions modules/020-deckhouse/hooks/stabilize_release_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (
)

var _ = Describe("Modules :: deckhouse :: hooks :: stabilize release channel ::", func() {

Context("releaseChannel type", func() {
It("switches release channels by increment", func() {

rc := releaseChannel(-2)
Expect(rc.IsKnown()).To(BeFalse())
Expect(rc.Tag()).To(Equal(""))
Expand Down Expand Up @@ -374,5 +372,4 @@ var _ = Describe("Modules :: deckhouse :: hooks :: stabilize release channel ::"
table.Entry("Alpha -> RockSolid", nameAlpha, nameRockSolid),
)
})

})

0 comments on commit 34e96fb

Please sign in to comment.