Skip to content

Commit 72be29c

Browse files
authored
[Feature] Discover Namespace in DebugPackage from K8S (#1623)
1 parent 31ede22 commit 72be29c

File tree

6 files changed

+204
-8
lines changed

6 files changed

+204
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- (Feature) Extend Backup Details in DebugPackage
77
- (Feature) (ML) Use Scheduler API
88
- (Feature) (Scheduler) Introduce Scheduler CRD
9+
- (Feature) Discover Namespace in DebugPackage from K8S
910

1011
## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
1112
- (Feature) Extract Scheduler API

cmd/debug.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -32,16 +32,25 @@ import (
3232

3333
func init() {
3434
cmdMain.AddCommand(debugPackage)
35+
cmdMain.AddCommand(debugPackageV2)
3536

36-
f := debugPackage.Flags()
37-
38-
f.StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
37+
debugPackage.Flags().StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
38+
debugPackageV2.Flags().StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
3939

4040
debug_package.InitCommand(debugPackage)
41+
debug_package.InitCommand(debugPackageV2)
4142
}
4243

4344
var debugPackage = &cobra.Command{
44-
Use: "debugPackage",
45+
Use: "debugPackage",
46+
Short: "Generate debug package for debugging",
47+
RunE: debugPackageFunc,
48+
Hidden: true,
49+
Deprecated: "Use debug-package command instead",
50+
}
51+
52+
var debugPackageV2 = &cobra.Command{
53+
Use: "debug-package",
4554
Short: "Generate debug package for debugging",
4655
RunE: debugPackageFunc,
4756
}

pkg/debug_package/cli/cli.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -20,11 +20,15 @@
2020

2121
package cli
2222

23-
import "github.com/spf13/cobra"
23+
import (
24+
"github.com/spf13/cobra"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/constants"
27+
)
2428

2529
func Register(cmd *cobra.Command) {
2630
f := cmd.Flags()
27-
f.StringVarP(&input.Namespace, "namespace", "n", "default", "Kubernetes namespace")
31+
f.StringVarP(&input.Namespace, "namespace", "n", constants.NamespaceWithDefault("default"), "Kubernetes namespace")
2832
f.BoolVar(&input.HideSensitiveData, "hide-sensitive-data", true, "Hide sensitive data")
2933
f.BoolVar(&input.PodLogs, "pod-logs", true, "Collect pod logs")
3034
}

pkg/util/constants/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const (
3131
EnvOperatorPodIP = "MY_POD_IP"
3232
EnvArangoJobSAName = "ARANGOJOB_SA_NAME"
3333

34+
PathMountServiceAccount = "/var/run/secrets/kubernetes.io/serviceaccount"
35+
PathMountServiceAccountNamespace = PathMountServiceAccount + "/namespace"
36+
3437
EnvArangoLicenseKey = "ARANGO_LICENSE_KEY" // Contains the License Key for the Docker Image
3538
EnvArangoSyncMonitoringToken = "ARANGOSYNC_MONITORING_TOKEN" // Constains monitoring token for ArangoSync servers
3639

pkg/util/constants/namespace.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2024 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 constants
22+
23+
import (
24+
"os"
25+
"strings"
26+
)
27+
28+
func NamespaceWithDefault(def string) string {
29+
if v, ok := Namespace(); ok {
30+
return v
31+
}
32+
33+
return def
34+
}
35+
36+
func Namespace() (string, bool) {
37+
return namespaceWithSAAndEnv(PathMountServiceAccountNamespace, EnvOperatorPodNamespace)
38+
}
39+
40+
func namespaceWithSAAndEnv(sa, env string) (string, bool) {
41+
// Extract from env
42+
if e, ok := os.LookupEnv(env); ok && e != "" {
43+
if v := strings.TrimSpace(e); v != "" {
44+
return v, true
45+
}
46+
}
47+
48+
// Extract from file
49+
if data, err := os.ReadFile(sa); err == nil && len(data) > 0 {
50+
if v := strings.TrimSpace(string(data)); v != "" {
51+
return v, true
52+
}
53+
}
54+
55+
return "", false
56+
}

pkg/util/constants/namespace_test.go

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2024 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 constants
22+
23+
import (
24+
"fmt"
25+
"os"
26+
"path"
27+
"strings"
28+
"testing"
29+
30+
"github.com/dchest/uniuri"
31+
"github.com/stretchr/testify/require"
32+
)
33+
34+
func withFile(t *testing.T, ns string) func(in func(file string)) {
35+
return func(in func(key string)) {
36+
p := t.TempDir()
37+
38+
ret := path.Join(p, strings.ToLower(uniuri.NewLen(32)))
39+
40+
require.NoError(t, os.WriteFile(ret, []byte(ns), 0644))
41+
42+
in(ret)
43+
}
44+
}
45+
46+
func withEnv(t *testing.T, ns string) func(in func(env string)) {
47+
return func(in func(key string)) {
48+
key := fmt.Sprintf("MY_NS_ENV_%s", strings.ToUpper(uniuri.NewLen(8)))
49+
50+
require.NoError(t, os.Setenv(key, ns))
51+
defer func() {
52+
require.NoError(t, os.Unsetenv(key))
53+
}()
54+
55+
in(key)
56+
}
57+
}
58+
59+
func Test_Namespace(t *testing.T) {
60+
t.Run("Default", func(t *testing.T) {
61+
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, EnvOperatorPodNamespace)
62+
require.False(t, ok)
63+
require.EqualValues(t, "", n)
64+
})
65+
t.Run("With Env", func(t *testing.T) {
66+
withEnv(t, "myNs1")(func(env string) {
67+
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
68+
require.True(t, ok)
69+
require.EqualValues(t, "myNs1", n)
70+
})
71+
})
72+
t.Run("With Empty Env", func(t *testing.T) {
73+
withEnv(t, "")(func(env string) {
74+
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
75+
require.False(t, ok)
76+
require.EqualValues(t, "", n)
77+
})
78+
})
79+
t.Run("With Whitespace Env", func(t *testing.T) {
80+
withEnv(t, " \n ")(func(env string) {
81+
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
82+
require.False(t, ok)
83+
require.EqualValues(t, "", n)
84+
})
85+
})
86+
t.Run("With File", func(t *testing.T) {
87+
withFile(t, "myNs2")(func(file string) {
88+
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
89+
require.True(t, ok)
90+
require.EqualValues(t, "myNs2", n)
91+
})
92+
})
93+
t.Run("With Missing File", func(t *testing.T) {
94+
withFile(t, "myNs2")(func(file string) {
95+
n, ok := namespaceWithSAAndEnv(fmt.Sprintf("%s.missing", file), EnvOperatorPodNamespace)
96+
require.False(t, ok)
97+
require.EqualValues(t, "", n)
98+
})
99+
})
100+
t.Run("With Empty File", func(t *testing.T) {
101+
withFile(t, "")(func(file string) {
102+
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
103+
require.False(t, ok)
104+
require.EqualValues(t, "", n)
105+
})
106+
})
107+
t.Run("With Whitespace File", func(t *testing.T) {
108+
withFile(t, " \n ")(func(file string) {
109+
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
110+
require.False(t, ok)
111+
require.EqualValues(t, "", n)
112+
})
113+
})
114+
t.Run("With File & Env", func(t *testing.T) {
115+
withFile(t, "myNs2")(func(file string) {
116+
withEnv(t, "myNs1")(func(env string) {
117+
n, ok := namespaceWithSAAndEnv(file, env)
118+
require.True(t, ok)
119+
require.EqualValues(t, "myNs1", n)
120+
})
121+
})
122+
})
123+
}

0 commit comments

Comments
 (0)