Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Discover Namespace in DebugPackage from K8S #1623

Merged
merged 1 commit into from
Mar 18, 2024
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 @@
- (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
Expand Down
19 changes: 14 additions & 5 deletions cmd/debug.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/debug_package/cli/cli.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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")
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions pkg/util/constants/namespace.go
Original file line number Diff line number Diff line change
@@ -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
}
123 changes: 123 additions & 0 deletions pkg/util/constants/namespace_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
}