1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Feature] Discover Namespace in DebugPackage from K8S (#1623)

This commit is contained in:
Adam Janikowski 2024-03-18 12:12:09 +01:00 committed by GitHub
parent 31ede22043
commit 72be29c602
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 204 additions and 8 deletions

View file

@ -6,6 +6,7 @@
- (Feature) Extend Backup Details in DebugPackage
- (Feature) (ML) Use Scheduler API
- (Feature) (Scheduler) Introduce Scheduler CRD
- (Feature) Discover Namespace in DebugPackage from K8S
## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
- (Feature) Extract Scheduler API

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2024 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.
@ -32,16 +32,25 @@ import (
func init() {
cmdMain.AddCommand(debugPackage)
cmdMain.AddCommand(debugPackageV2)
f := debugPackage.Flags()
f.StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
debugPackage.Flags().StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
debugPackageV2.Flags().StringVarP(&debugPackageInput.Output, "output", "o", "out.tar.gz", "Output of the result gz file. If set to `-` then stdout is used")
debug_package.InitCommand(debugPackage)
debug_package.InitCommand(debugPackageV2)
}
var debugPackage = &cobra.Command{
Use: "debugPackage",
Use: "debugPackage",
Short: "Generate debug package for debugging",
RunE: debugPackageFunc,
Hidden: true,
Deprecated: "Use debug-package command instead",
}
var debugPackageV2 = &cobra.Command{
Use: "debug-package",
Short: "Generate debug package for debugging",
RunE: debugPackageFunc,
}

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2024 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.
@ -20,11 +20,15 @@
package cli
import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
)
func Register(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&input.Namespace, "namespace", "n", "default", "Kubernetes namespace")
f.StringVarP(&input.Namespace, "namespace", "n", constants.NamespaceWithDefault("default"), "Kubernetes namespace")
f.BoolVar(&input.HideSensitiveData, "hide-sensitive-data", true, "Hide sensitive data")
f.BoolVar(&input.PodLogs, "pod-logs", true, "Collect pod logs")
}

View file

@ -31,6 +31,9 @@ const (
EnvOperatorPodIP = "MY_POD_IP"
EnvArangoJobSAName = "ARANGOJOB_SA_NAME"
PathMountServiceAccount = "/var/run/secrets/kubernetes.io/serviceaccount"
PathMountServiceAccountNamespace = PathMountServiceAccount + "/namespace"
EnvArangoLicenseKey = "ARANGO_LICENSE_KEY" // Contains the License Key for the Docker Image
EnvArangoSyncMonitoringToken = "ARANGOSYNC_MONITORING_TOKEN" // Constains monitoring token for ArangoSync servers

View file

@ -0,0 +1,56 @@
//
// DISCLAIMER
//
// Copyright 2024 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 constants
import (
"os"
"strings"
)
func NamespaceWithDefault(def string) string {
if v, ok := Namespace(); ok {
return v
}
return def
}
func Namespace() (string, bool) {
return namespaceWithSAAndEnv(PathMountServiceAccountNamespace, EnvOperatorPodNamespace)
}
func namespaceWithSAAndEnv(sa, env string) (string, bool) {
// Extract from env
if e, ok := os.LookupEnv(env); ok && e != "" {
if v := strings.TrimSpace(e); v != "" {
return v, true
}
}
// Extract from file
if data, err := os.ReadFile(sa); err == nil && len(data) > 0 {
if v := strings.TrimSpace(string(data)); v != "" {
return v, true
}
}
return "", false
}

View file

@ -0,0 +1,123 @@
//
// DISCLAIMER
//
// Copyright 2024 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 constants
import (
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/dchest/uniuri"
"github.com/stretchr/testify/require"
)
func withFile(t *testing.T, ns string) func(in func(file string)) {
return func(in func(key string)) {
p := t.TempDir()
ret := path.Join(p, strings.ToLower(uniuri.NewLen(32)))
require.NoError(t, os.WriteFile(ret, []byte(ns), 0644))
in(ret)
}
}
func withEnv(t *testing.T, ns string) func(in func(env string)) {
return func(in func(key string)) {
key := fmt.Sprintf("MY_NS_ENV_%s", strings.ToUpper(uniuri.NewLen(8)))
require.NoError(t, os.Setenv(key, ns))
defer func() {
require.NoError(t, os.Unsetenv(key))
}()
in(key)
}
}
func Test_Namespace(t *testing.T) {
t.Run("Default", func(t *testing.T) {
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, EnvOperatorPodNamespace)
require.False(t, ok)
require.EqualValues(t, "", n)
})
t.Run("With Env", func(t *testing.T) {
withEnv(t, "myNs1")(func(env string) {
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
require.True(t, ok)
require.EqualValues(t, "myNs1", n)
})
})
t.Run("With Empty Env", func(t *testing.T) {
withEnv(t, "")(func(env string) {
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
require.False(t, ok)
require.EqualValues(t, "", n)
})
})
t.Run("With Whitespace Env", func(t *testing.T) {
withEnv(t, " \n ")(func(env string) {
n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env)
require.False(t, ok)
require.EqualValues(t, "", n)
})
})
t.Run("With File", func(t *testing.T) {
withFile(t, "myNs2")(func(file string) {
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
require.True(t, ok)
require.EqualValues(t, "myNs2", n)
})
})
t.Run("With Missing File", func(t *testing.T) {
withFile(t, "myNs2")(func(file string) {
n, ok := namespaceWithSAAndEnv(fmt.Sprintf("%s.missing", file), EnvOperatorPodNamespace)
require.False(t, ok)
require.EqualValues(t, "", n)
})
})
t.Run("With Empty File", func(t *testing.T) {
withFile(t, "")(func(file string) {
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
require.False(t, ok)
require.EqualValues(t, "", n)
})
})
t.Run("With Whitespace File", func(t *testing.T) {
withFile(t, " \n ")(func(file string) {
n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace)
require.False(t, ok)
require.EqualValues(t, "", n)
})
})
t.Run("With File & Env", func(t *testing.T) {
withFile(t, "myNs2")(func(file string) {
withEnv(t, "myNs1")(func(env string) {
n, ok := namespaceWithSAAndEnv(file, env)
require.True(t, ok)
require.EqualValues(t, "myNs1", n)
})
})
})
}