mirror of https://github.com/fluxcd/cli-utils.git
				
				
				
			namespaced kpt live init
This commit is contained in:
		
							parent
							
								
									7d237621fd
								
							
						
					
					
						commit
						a6f98bc844
					
				| 
						 | 
				
			
			@ -25,5 +25,6 @@ func NewCmdInit(ioStreams genericclioptions.IOStreams) *cobra.Command {
 | 
			
		|||
		},
 | 
			
		||||
	}
 | 
			
		||||
	cmd.Flags().StringVarP(&io.InventoryID, "inventory-id", "i", "", "Identifier for group of applied resources. Must be composed of valid label characters.")
 | 
			
		||||
	cmd.Flags().StringVarP(&io.Namespace, "inventory-namespace", "", "", "namespace for the resources to be initialized")
 | 
			
		||||
	return cmd
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,11 +11,11 @@ import (
 | 
			
		|||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-errors/errors"
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
			
		||||
	"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
			
		||||
	"sigs.k8s.io/kustomize/kyaml/kio"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const manifestFilename = "inventory-template.yaml"
 | 
			
		||||
| 
						 | 
				
			
			@ -142,19 +142,25 @@ func calcPackageNamespace(packageDir string) (string, error) {
 | 
			
		|||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	// Return the first non-empty namespace found. Cluster-scoped
 | 
			
		||||
	// Return the non-empty unique namespace if found. Cluster-scoped
 | 
			
		||||
	// resources do not have namespace set.
 | 
			
		||||
	currentNamespace := metav1.NamespaceDefault
 | 
			
		||||
	for _, node := range nodes {
 | 
			
		||||
		rm, err := node.GetMeta()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
		if err != nil || len(rm.ObjectMeta.Namespace) == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if len(rm.ObjectMeta.Namespace) > 0 {
 | 
			
		||||
			return rm.ObjectMeta.Namespace, nil
 | 
			
		||||
		if currentNamespace == metav1.NamespaceDefault {
 | 
			
		||||
			currentNamespace = rm.ObjectMeta.Namespace
 | 
			
		||||
		}
 | 
			
		||||
		if currentNamespace != rm.ObjectMeta.Namespace {
 | 
			
		||||
			return "", errors.Errorf(
 | 
			
		||||
				"resources belong to different namespaces, a namespace is required to create the resource " +
 | 
			
		||||
					"used for keeping track of past apply operations. Please specify ---inv-namespace.")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// Return the default namespace if none found.
 | 
			
		||||
	return metav1.NamespaceDefault, nil
 | 
			
		||||
	return currentNamespace, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// defaultInventoryID returns a UUID string as a default unique
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,16 +5,66 @@ package config
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ioStreams = genericclioptions.IOStreams{}
 | 
			
		||||
 | 
			
		||||
// writeFile writes a file under the test directory
 | 
			
		||||
func writeFile(t *testing.T, path string, value []byte) {
 | 
			
		||||
	err := ioutil.WriteFile(path, value, 0600)
 | 
			
		||||
	if !assert.NoError(t, err) {
 | 
			
		||||
		assert.FailNow(t, err.Error())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var readFileA = []byte(`
 | 
			
		||||
apiVersion: v1
 | 
			
		||||
kind: Pod
 | 
			
		||||
metadata:
 | 
			
		||||
  name: objA
 | 
			
		||||
  namespace: namespaceA
 | 
			
		||||
`)
 | 
			
		||||
 | 
			
		||||
var readFileB = []byte(`
 | 
			
		||||
apiVersion: v1
 | 
			
		||||
kind: Pod
 | 
			
		||||
metadata:
 | 
			
		||||
  name: objB
 | 
			
		||||
  namespace: namespaceB
 | 
			
		||||
`)
 | 
			
		||||
 | 
			
		||||
var readFileC = []byte(`
 | 
			
		||||
apiVersion: v1
 | 
			
		||||
kind: Pod
 | 
			
		||||
metadata:
 | 
			
		||||
  name: objC
 | 
			
		||||
`)
 | 
			
		||||
 | 
			
		||||
func TestComplete(t *testing.T) {
 | 
			
		||||
	d1, err := ioutil.TempDir("", "test-dir")
 | 
			
		||||
	if !assert.NoError(t, err) {
 | 
			
		||||
		assert.FailNow(t, err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer os.RemoveAll(d1)
 | 
			
		||||
	d2, err := ioutil.TempDir("", "test-dir")
 | 
			
		||||
	if !assert.NoError(t, err) {
 | 
			
		||||
		assert.FailNow(t, err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer os.RemoveAll(d2)
 | 
			
		||||
 | 
			
		||||
	writeFile(t, filepath.Join(d1, "a_test.yaml"), readFileA)
 | 
			
		||||
	writeFile(t, filepath.Join(d1, "b_test.yaml"), readFileB)
 | 
			
		||||
	writeFile(t, filepath.Join(d2, "b_test.yaml"), readFileC)
 | 
			
		||||
 | 
			
		||||
	tests := map[string]struct {
 | 
			
		||||
		args    []string
 | 
			
		||||
		isError bool
 | 
			
		||||
| 
						 | 
				
			
			@ -31,8 +81,15 @@ func TestComplete(t *testing.T) {
 | 
			
		|||
			args:    []string{"foo"},
 | 
			
		||||
			isError: true,
 | 
			
		||||
		},
 | 
			
		||||
		"More than one namespace should fail": {
 | 
			
		||||
			args:    []string{d1},
 | 
			
		||||
			isError: true,
 | 
			
		||||
		},
 | 
			
		||||
		"No namespace set is fine": {
 | 
			
		||||
			args:    []string{d2},
 | 
			
		||||
			isError: false,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for name, tc := range tests {
 | 
			
		||||
		t.Run(name, func(t *testing.T) {
 | 
			
		||||
			io := NewInitOptions(ioStreams)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue