Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- (Refactor) Anonymous inspector functions
- (Feature) Recursive OwnerReference discovery
- (Maintenance) Add check make targets
- (Feature) Create support for local variables in actions.

## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30)
- (Bugfix) Orphan PVC are not removed
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ifeq ($(DEBUG),true)
DEBUG := true
DOCKERFILE := Dockerfile.debug
# required by DLV https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_exec.md
COMPILE_DEBUG_FLAGS := -gcflags="all=-N -l"
COMPILE_DEBUG_FLAGS := -gcflags="all=-N -l" -ldflags "-extldflags '-static'"
else
DEBUG := false
DOCKERFILE := Dockerfile
Expand Down Expand Up @@ -522,4 +522,4 @@ check-community:
@$(MAKE) _check RELEASE_MODE=community

_check:
@$(MAKE) fmt license-verify linter run-unit-tests bin
@$(MAKE) fmt license-verify linter run-unit-tests bin
8 changes: 6 additions & 2 deletions pkg/apis/deployment/v1/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
package v1

import (
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/dchest/uniuri"
"k8s.io/apimachinery/pkg/api/equality"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"

"github.com/arangodb/kube-arangodb/pkg/util"
)

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

// Equal compares two Actions
Expand All @@ -237,7 +240,8 @@ func (a Action) Equal(other Action) bool {
util.TimeCompareEqualPointer(a.StartTime, other.StartTime) &&
a.Reason == other.Reason &&
a.Image == other.Image &&
equality.Semantic.DeepEqual(a.Params, other.Params)
equality.Semantic.DeepEqual(a.Params, other.Params) &&
a.Locals.Equal(other.Locals)
}

// AddParam returns copy of action with set parameter
Expand Down
114 changes: 114 additions & 0 deletions pkg/apis/deployment/v1/plan_locals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

type PlanLocalKey string

func (p PlanLocalKey) String() string {
return string(p)
}

type PlanLocals map[PlanLocalKey]string

func (p *PlanLocals) Remove(key PlanLocalKey) bool {
if *p == nil {
return false
}

z := *p

if _, ok := z[key]; ok {
delete(z, key)
*p = z
return true
}

return false
}

func (p PlanLocals) Get(key PlanLocalKey) (string, bool) {
v, ok := p[key]
return v, ok
}

func (p PlanLocals) GetWithParent(parent PlanLocals, key PlanLocalKey) (string, bool) {
v, ok := p[key]
if ok {
return v, true
}
return parent.Get(key)
}

func (p *PlanLocals) Merge(merger PlanLocals) (changed bool) {
for k, v := range merger {
if p.Add(k, v, true) {
changed = true
}
}

return
}

func (p *PlanLocals) Add(key PlanLocalKey, value string, override bool) bool {
if value == "" {
return p.Remove(key)
}

if *p == nil {
*p = PlanLocals{
key: value,
}

return true
}

z := *p

if v, ok := z[key]; ok {
if v == value {
return true
}

if !override {
return false
}
}

z[key] = value

*p = z

return true
}

func (p PlanLocals) Equal(other PlanLocals) bool {
if len(p) != len(other) {
return false
}

for k, v := range p {
if v2, ok := other[k]; !ok || v != v2 {
return false
}
}

return true
}
148 changes: 148 additions & 0 deletions pkg/apis/deployment/v1/plan_locals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_PlanLocals(t *testing.T) {
var l PlanLocals

var key PlanLocalKey = "test"
v1, v2 := "v1", "v2"

t.Run("Get on nil", func(t *testing.T) {
v, ok := l.Get(key)

require.Equal(t, "", v)
require.False(t, ok)
})

t.Run("Remove on nil", func(t *testing.T) {
ok := l.Remove(key)

require.False(t, ok)
})

t.Run("Add", func(t *testing.T) {
ok := l.Add(key, v1, false)

require.True(t, ok)

v, ok := l.Get(key)

require.True(t, ok)
require.Equal(t, v1, v)
})

t.Run("Update", func(t *testing.T) {
ok := l.Add(key, v2, false)

require.False(t, ok)

v, ok := l.Get(key)

require.True(t, ok)
require.Equal(t, v1, v)
})

t.Run("Update - override", func(t *testing.T) {
ok := l.Add(key, v2, true)

require.True(t, ok)

v, ok := l.Get(key)

require.True(t, ok)
require.Equal(t, v2, v)
})

t.Run("Remove", func(t *testing.T) {
ok := l.Remove(key)

require.True(t, ok)
})

t.Run("Remove missing", func(t *testing.T) {
ok := l.Remove(key)

require.False(t, ok)
})
}

func Test_PlanLocals_Equal(t *testing.T) {
cmp := func(name string, a, b PlanLocals, expected bool) {
t.Run(name, func(t *testing.T) {
require.True(t, a.Equal(a))
require.True(t, b.Equal(b))
if expected {
require.True(t, a.Equal(b))
require.True(t, b.Equal(a))
} else {
require.False(t, a.Equal(b))
require.False(t, b.Equal(a))
}
})
}

cmp("Nil", nil, nil, true)

cmp("Nil & empty", nil, PlanLocals{}, true)

cmp("Empty", PlanLocals{}, PlanLocals{}, true)

cmp("Same keys & values", PlanLocals{
"key1": "v1",
}, PlanLocals{
"key1": "v1",
}, true)

cmp("Diff keys", PlanLocals{
"key2": "v1",
}, PlanLocals{
"key1": "v1",
}, false)

cmp("Same keys & diff values", PlanLocals{
"key1": "v1",
}, PlanLocals{
"key1": "v2",
}, false)

cmp("Same multi keys & values", PlanLocals{
"key1": "v1",
"ket2": "v2",
}, PlanLocals{
"key1": "v1",
"ket2": "v2",
}, true)

cmp("Same multi keys & values - reorder", PlanLocals{
"key1": "v1",
"ket2": "v2",
}, PlanLocals{
"ket2": "v2",
"key1": "v1",
}, true)
}
29 changes: 29 additions & 0 deletions pkg/apis/deployment/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions pkg/apis/deployment/v2alpha1/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
package v2alpha1

import (
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/dchest/uniuri"
"k8s.io/apimachinery/pkg/api/equality"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"

"github.com/arangodb/kube-arangodb/pkg/util"
)

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

// Equal compares two Actions
Expand All @@ -237,7 +240,8 @@ func (a Action) Equal(other Action) bool {
util.TimeCompareEqualPointer(a.StartTime, other.StartTime) &&
a.Reason == other.Reason &&
a.Image == other.Image &&
equality.Semantic.DeepEqual(a.Params, other.Params)
equality.Semantic.DeepEqual(a.Params, other.Params) &&
a.Locals.Equal(other.Locals)
}

// AddParam returns copy of action with set parameter
Expand Down
Loading