Skip to content

Commit aad986f

Browse files
authored
[Feature] Create local variables for an action (#978)
1 parent 66ff115 commit aad986f

File tree

14 files changed

+633
-12
lines changed

14 files changed

+633
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- (Refactor) Anonymous inspector functions
77
- (Feature) Recursive OwnerReference discovery
88
- (Maintenance) Add check make targets
9+
- (Feature) Create support for local variables in actions.
910

1011
## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30)
1112
- (Bugfix) Orphan PVC are not removed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ifeq ($(DEBUG),true)
8282
DEBUG := true
8383
DOCKERFILE := Dockerfile.debug
8484
# required by DLV https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_exec.md
85-
COMPILE_DEBUG_FLAGS := -gcflags="all=-N -l"
85+
COMPILE_DEBUG_FLAGS := -gcflags="all=-N -l" -ldflags "-extldflags '-static'"
8686
else
8787
DEBUG := false
8888
DOCKERFILE := Dockerfile
@@ -522,4 +522,4 @@ check-community:
522522
@$(MAKE) _check RELEASE_MODE=community
523523

524524
_check:
525-
@$(MAKE) fmt license-verify linter run-unit-tests bin
525+
@$(MAKE) fmt license-verify linter run-unit-tests bin

pkg/apis/deployment/v1/plan.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
package v1
2222

2323
import (
24-
"github.com/arangodb/kube-arangodb/pkg/util"
2524
"github.com/dchest/uniuri"
2625
"k8s.io/apimachinery/pkg/api/equality"
2726
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
"k8s.io/apimachinery/pkg/types"
2928
"k8s.io/apimachinery/pkg/util/uuid"
29+
30+
"github.com/arangodb/kube-arangodb/pkg/util"
3031
)
3132

3233
// ActionPriority define action priority
@@ -225,6 +226,8 @@ type Action struct {
225226
Image string `json:"image,omitempty"`
226227
// Params additional parameters used for action
227228
Params map[string]string `json:"params,omitempty"`
229+
// Locals additional storage for local variables which are produced during the action.
230+
Locals PlanLocals `json:"locals,omitempty"`
228231
}
229232

230233
// Equal compares two Actions
@@ -237,7 +240,8 @@ func (a Action) Equal(other Action) bool {
237240
util.TimeCompareEqualPointer(a.StartTime, other.StartTime) &&
238241
a.Reason == other.Reason &&
239242
a.Image == other.Image &&
240-
equality.Semantic.DeepEqual(a.Params, other.Params)
243+
equality.Semantic.DeepEqual(a.Params, other.Params) &&
244+
a.Locals.Equal(other.Locals)
241245
}
242246

243247
// AddParam returns copy of action with set parameter
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
type PlanLocalKey string
24+
25+
func (p PlanLocalKey) String() string {
26+
return string(p)
27+
}
28+
29+
type PlanLocals map[PlanLocalKey]string
30+
31+
func (p *PlanLocals) Remove(key PlanLocalKey) bool {
32+
if *p == nil {
33+
return false
34+
}
35+
36+
z := *p
37+
38+
if _, ok := z[key]; ok {
39+
delete(z, key)
40+
*p = z
41+
return true
42+
}
43+
44+
return false
45+
}
46+
47+
func (p PlanLocals) Get(key PlanLocalKey) (string, bool) {
48+
v, ok := p[key]
49+
return v, ok
50+
}
51+
52+
func (p PlanLocals) GetWithParent(parent PlanLocals, key PlanLocalKey) (string, bool) {
53+
v, ok := p[key]
54+
if ok {
55+
return v, true
56+
}
57+
return parent.Get(key)
58+
}
59+
60+
func (p *PlanLocals) Merge(merger PlanLocals) (changed bool) {
61+
for k, v := range merger {
62+
if p.Add(k, v, true) {
63+
changed = true
64+
}
65+
}
66+
67+
return
68+
}
69+
70+
func (p *PlanLocals) Add(key PlanLocalKey, value string, override bool) bool {
71+
if value == "" {
72+
return p.Remove(key)
73+
}
74+
75+
if *p == nil {
76+
*p = PlanLocals{
77+
key: value,
78+
}
79+
80+
return true
81+
}
82+
83+
z := *p
84+
85+
if v, ok := z[key]; ok {
86+
if v == value {
87+
return true
88+
}
89+
90+
if !override {
91+
return false
92+
}
93+
}
94+
95+
z[key] = value
96+
97+
*p = z
98+
99+
return true
100+
}
101+
102+
func (p PlanLocals) Equal(other PlanLocals) bool {
103+
if len(p) != len(other) {
104+
return false
105+
}
106+
107+
for k, v := range p {
108+
if v2, ok := other[k]; !ok || v != v2 {
109+
return false
110+
}
111+
}
112+
113+
return true
114+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
import (
24+
"testing"
25+
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func Test_PlanLocals(t *testing.T) {
30+
var l PlanLocals
31+
32+
var key PlanLocalKey = "test"
33+
v1, v2 := "v1", "v2"
34+
35+
t.Run("Get on nil", func(t *testing.T) {
36+
v, ok := l.Get(key)
37+
38+
require.Equal(t, "", v)
39+
require.False(t, ok)
40+
})
41+
42+
t.Run("Remove on nil", func(t *testing.T) {
43+
ok := l.Remove(key)
44+
45+
require.False(t, ok)
46+
})
47+
48+
t.Run("Add", func(t *testing.T) {
49+
ok := l.Add(key, v1, false)
50+
51+
require.True(t, ok)
52+
53+
v, ok := l.Get(key)
54+
55+
require.True(t, ok)
56+
require.Equal(t, v1, v)
57+
})
58+
59+
t.Run("Update", func(t *testing.T) {
60+
ok := l.Add(key, v2, false)
61+
62+
require.False(t, ok)
63+
64+
v, ok := l.Get(key)
65+
66+
require.True(t, ok)
67+
require.Equal(t, v1, v)
68+
})
69+
70+
t.Run("Update - override", func(t *testing.T) {
71+
ok := l.Add(key, v2, true)
72+
73+
require.True(t, ok)
74+
75+
v, ok := l.Get(key)
76+
77+
require.True(t, ok)
78+
require.Equal(t, v2, v)
79+
})
80+
81+
t.Run("Remove", func(t *testing.T) {
82+
ok := l.Remove(key)
83+
84+
require.True(t, ok)
85+
})
86+
87+
t.Run("Remove missing", func(t *testing.T) {
88+
ok := l.Remove(key)
89+
90+
require.False(t, ok)
91+
})
92+
}
93+
94+
func Test_PlanLocals_Equal(t *testing.T) {
95+
cmp := func(name string, a, b PlanLocals, expected bool) {
96+
t.Run(name, func(t *testing.T) {
97+
require.True(t, a.Equal(a))
98+
require.True(t, b.Equal(b))
99+
if expected {
100+
require.True(t, a.Equal(b))
101+
require.True(t, b.Equal(a))
102+
} else {
103+
require.False(t, a.Equal(b))
104+
require.False(t, b.Equal(a))
105+
}
106+
})
107+
}
108+
109+
cmp("Nil", nil, nil, true)
110+
111+
cmp("Nil & empty", nil, PlanLocals{}, true)
112+
113+
cmp("Empty", PlanLocals{}, PlanLocals{}, true)
114+
115+
cmp("Same keys & values", PlanLocals{
116+
"key1": "v1",
117+
}, PlanLocals{
118+
"key1": "v1",
119+
}, true)
120+
121+
cmp("Diff keys", PlanLocals{
122+
"key2": "v1",
123+
}, PlanLocals{
124+
"key1": "v1",
125+
}, false)
126+
127+
cmp("Same keys & diff values", PlanLocals{
128+
"key1": "v1",
129+
}, PlanLocals{
130+
"key1": "v2",
131+
}, false)
132+
133+
cmp("Same multi keys & values", PlanLocals{
134+
"key1": "v1",
135+
"ket2": "v2",
136+
}, PlanLocals{
137+
"key1": "v1",
138+
"ket2": "v2",
139+
}, true)
140+
141+
cmp("Same multi keys & values - reorder", PlanLocals{
142+
"key1": "v1",
143+
"ket2": "v2",
144+
}, PlanLocals{
145+
"ket2": "v2",
146+
"key1": "v1",
147+
}, true)
148+
}

pkg/apis/deployment/v1/zz_generated.deepcopy.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/deployment/v2alpha1/plan.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
package v2alpha1
2222

2323
import (
24-
"github.com/arangodb/kube-arangodb/pkg/util"
2524
"github.com/dchest/uniuri"
2625
"k8s.io/apimachinery/pkg/api/equality"
2726
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
"k8s.io/apimachinery/pkg/types"
2928
"k8s.io/apimachinery/pkg/util/uuid"
29+
30+
"github.com/arangodb/kube-arangodb/pkg/util"
3031
)
3132

3233
// ActionPriority define action priority
@@ -225,6 +226,8 @@ type Action struct {
225226
Image string `json:"image,omitempty"`
226227
// Params additional parameters used for action
227228
Params map[string]string `json:"params,omitempty"`
229+
// Locals additional storage for local variables which are produced during the action.
230+
Locals PlanLocals `json:"locals,omitempty"`
228231
}
229232

230233
// Equal compares two Actions
@@ -237,7 +240,8 @@ func (a Action) Equal(other Action) bool {
237240
util.TimeCompareEqualPointer(a.StartTime, other.StartTime) &&
238241
a.Reason == other.Reason &&
239242
a.Image == other.Image &&
240-
equality.Semantic.DeepEqual(a.Params, other.Params)
243+
equality.Semantic.DeepEqual(a.Params, other.Params) &&
244+
a.Locals.Equal(other.Locals)
241245
}
242246

243247
// AddParam returns copy of action with set parameter

0 commit comments

Comments
 (0)