Skip to content

Commit

Permalink
feat(multiple): moving some cache keys to annotations base, add kuber…
Browse files Browse the repository at this point in the history
…netes client to runner
  • Loading branch information
Alan-pad committed Dec 26, 2022
1 parent 3672cf7 commit 17b3d15
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 112 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ COPY controllers/ controllers/
COPY burrito/ burrito/
COPY cmd/ cmd/
COPY runner/ runner/
COPY cache/ cache
COPY cache/ cache/
COPY annotations/ annotations/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
11 changes: 10 additions & 1 deletion cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ func NewRedisCache(addr string, password string, db int) *RedisCache {

func (r *RedisCache) Get(key string) ([]byte, error) {
val, err := r.Client.Get(context.TODO(), key).Result()
if err == redis.Nil {
return nil, &CacheError{
Err: err,
Nil: true,
}
}
if err != nil {
return nil, err
return nil, &CacheError{
Err: err,
Nil: false,
}
}
return []byte(val), nil
}
Expand Down
70 changes: 33 additions & 37 deletions controllers/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package controllers

import (
"context"
"strconv"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/padok-team/burrito/annotations"
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
internal "github.com/padok-team/burrito/cache"

Expand Down Expand Up @@ -64,6 +66,7 @@ var _ = Describe("TerraformLayer", func() {
},
},
}
t.SetAnnotations(map[string]string{})
cache = internal.NewMemoryCache()
})

Expand Down Expand Up @@ -96,13 +99,13 @@ var _ = Describe("TerraformLayer", func() {
})
Context("with last timestamp in cache < 20min", func() {
It("should return true", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int((time.Now().Add(-5 * time.Minute)).Unix()))), 0)
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 15).Unix()))
Expect(condition.Evaluate(cache, t)).To(Equal(true))
})
})
Context("with last timestamp in cache > 20min", func() {
It("should return false", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int(time.Now().Add(-time.Minute*60).Unix()))), 0)
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 60).Unix()))
Expect(condition.Evaluate(cache, t)).To(Equal(false))
})
})
Expand All @@ -119,21 +122,21 @@ var _ = Describe("TerraformLayer", func() {
})
Context("with plan in cache but no apply", func() {
It("should return false", func() {
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
Expect(condition.Evaluate(cache, t)).To(Equal(false))
})
})
Context("with same plan and apply in cache", func() {
It("should return true", func() {
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.LastAppliedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
t.Annotations[annotations.LastApplySum] = "ThisIsAPlanArtifact"
Expect(condition.Evaluate(cache, t)).To(Equal(true))
})
})
Context("with different plan and apply in cache", func() {
It("should return false", func() {
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.LastAppliedArtifact, t), []byte("ThisIsAnotherPlanArtifact"), 0)
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
t.Annotations[annotations.LastApplySum] = "ThisIsAnotherPlanArtifact"
Expect(condition.Evaluate(cache, t)).To(Equal(false))
})
})
Expand All @@ -150,14 +153,7 @@ var _ = Describe("TerraformLayer", func() {
})
Context("with terraform failure in cache", func() {
It("should return true", func() {
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("1"), 0)
Expect(condition.Evaluate(cache, t)).To(Equal(true))
})
})
Context("with terraform failure and message in cache", func() {
It("should return true", func() {
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("1"), 0)
cache.Set(internal.GenerateKey(internal.RunMessage, t), []byte("This is an error message."), 0)
t.Annotations[annotations.Failure] = "1"
Expect(condition.Evaluate(cache, t)).To(Equal(true))
})
})
Expand All @@ -170,28 +166,28 @@ var _ = Describe("TerraformLayer", func() {
Context("terraform is running", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.Lock, t), []byte{1}, 0)
_, out := conditions.Evaluate()
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionTrue))
})
})
Context("terraform not running and everything is up to date", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int((time.Now().Add(-5 * time.Minute)).Unix()))), 0)
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.LastAppliedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
_, out := conditions.Evaluate()
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 15).Unix()))
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
t.Annotations[annotations.LastApplySum] = "ThisIsAPlanArtifact"
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionFalse))
Expect(out[1].Status).To(Equal(metav1.ConditionTrue))
Expect(out[2].Status).To(Equal(metav1.ConditionTrue))
})
})
Context("terraform not running, plan up to date, apply not up to date, terraform has failed", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int((time.Now().Add(-5 * time.Minute)).Unix()))), 0)
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.LastAppliedArtifact, t), []byte("ThisIsAnotherPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("1"), 0)
_, out := conditions.Evaluate()
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 15).Unix()))
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
t.Annotations[annotations.LastApplySum] = "ThisIsAnotherPlanArtifact"
t.Annotations[annotations.Failure] = "1"
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionFalse))
Expect(out[1].Status).To(Equal(metav1.ConditionTrue))
Expect(out[2].Status).To(Equal(metav1.ConditionFalse))
Expand All @@ -200,11 +196,11 @@ var _ = Describe("TerraformLayer", func() {
})
Context("terraform not running, plan up to date, apply noy up to date, terraform has not failed", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int((time.Now().Add(-5 * time.Minute)).Unix()))), 0)
cache.Set(internal.GenerateKey(internal.LastPlannedArtifact, t), []byte("ThisIsAPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.LastAppliedArtifact, t), []byte("ThisIsAnotherPlanArtifact"), 0)
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("0"), 0)
_, out := conditions.Evaluate()
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 15).Unix()))
t.Annotations[annotations.LastPlanSum] = "ThisIsAPlanArtifact"
t.Annotations[annotations.LastApplySum] = "ThisIsAnotherPlanArtifact"
t.Annotations[annotations.Failure] = "0"
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionFalse))
Expect(out[1].Status).To(Equal(metav1.ConditionTrue))
Expect(out[2].Status).To(Equal(metav1.ConditionFalse))
Expand All @@ -213,19 +209,19 @@ var _ = Describe("TerraformLayer", func() {
})
Context("terraform not running, plan not up to date, terraform has failed", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int(time.Now().Add(-time.Minute*60).Unix()))), 0)
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("1"), 0)
_, out := conditions.Evaluate()
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 60).Unix()))
t.Annotations[annotations.Failure] = "1"
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionFalse))
Expect(out[1].Status).To(Equal(metav1.ConditionFalse))
Expect(out[3].Status).To(Equal(metav1.ConditionTrue))
})
})
Context("terraform not running, plan not up to date, terraform has failed", func() {
Context("terraform not running, plan not up to date, terraform hasn't failed", func() {
It("", func() {
cache.Set(internal.GenerateKey(internal.LastPlanDate, t), []byte(strconv.Itoa(int(time.Now().Add(-time.Minute*60).Unix()))), 0)
cache.Set(internal.GenerateKey(internal.RunResult, t), []byte("0"), 0)
_, out := conditions.Evaluate()
t.Annotations[annotations.LastPlanDate] = strconv.Itoa(int(time.Now().Add(-time.Minute * 60).Unix()))
t.Annotations[annotations.Failure] = "0"
_, out := conditions.Evaluate(context.TODO())
Expect(out[0].Status).To(Equal(metav1.ConditionFalse))
Expect(out[1].Status).To(Equal(metav1.ConditionFalse))
Expect(out[3].Status).To(Equal(metav1.ConditionFalse))
Expand Down
3 changes: 2 additions & 1 deletion controllers/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func defaultPodSpec(layer *configv1alpha1.TerraformLayer, repository *configv1al
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
},
RestartPolicy: corev1.RestartPolicyNever,
RestartPolicy: corev1.RestartPolicyNever,
ServiceAccountName: "burrito-runner",
Containers: []corev1.Container{
{
Name: "runner",
Expand Down
Loading

0 comments on commit 17b3d15

Please sign in to comment.