context-ns
Max Jonas Werner 1 year ago
parent 12efb1967e
commit a01e6a6c3b
No known key found for this signature in database
GPG Key ID: EB525E0F02B52140

@ -101,7 +101,7 @@ func createImageRepositoryRun(cmd *cobra.Command, args []string) error {
var repo = imagev1.ImageRepository{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
Namespace: *kubeconfigArgs.Namespace,
Namespace: GetDesiredNamespace(kubeconfigArgs),
Labels: labels,
},
Spec: imagev1.ImageRepositorySpec{

@ -146,9 +146,11 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error {
return err
}
ns := GetDesiredNamespace(kubeconfigArgs)
var listOpts []client.ListOption
if !getArgs.allNamespaces {
listOpts = append(listOpts, client.InNamespace(*kubeconfigArgs.Namespace))
listOpts = append(listOpts, client.InNamespace(ns))
}
if len(args) > 0 {
@ -190,12 +192,12 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error {
logger.Failuref("%s object '%s' not found in %s namespace",
get.kind,
args[0],
namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace),
namespaceNameOrAny(getArgs.allNamespaces, ns),
)
} else if !getAll {
logger.Failuref("no %s objects found in %s namespace",
get.kind,
namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace),
namespaceNameOrAny(getArgs.allNamespaces, ns),
)
}
return nil

@ -146,7 +146,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
opts := install.Options{
BaseURL: installArgs.manifestsPath,
Version: installArgs.version,
Namespace: *kubeconfigArgs.Namespace,
Namespace: GetDesiredNamespace(kubeconfigArgs),
Components: components,
Registry: installArgs.registry,
ImagePullSecret: installArgs.imagePullSecret,
@ -181,7 +181,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
}
logger.Successf("manifests build completed")
logger.Actionf("installing components in %s namespace", *kubeconfigArgs.Namespace)
logger.Actionf("installing components in %s namespace", opts.Namespace)
applyOutput, err := utils.Apply(ctx, kubeconfigArgs, kubeclientOptions, tmpDir, filepath.Join(tmpDir, manifest.Path))
if err != nil {

@ -105,6 +105,10 @@ Command line utility for assembling Kubernetes CD pipelines the GitOps way.`,
return fmt.Errorf("error getting namespace: %w", err)
}
if ns == "" {
return nil
}
if e := validation.IsDNS1123Label(ns); len(e) > 0 {
return fmt.Errorf("namespace must be a valid DNS label: %q", ns)
}
@ -140,7 +144,6 @@ func init() {
rootCmd.PersistentFlags().DurationVar(&rootArgs.timeout, "timeout", 5*time.Minute, "timeout for this operation")
rootCmd.PersistentFlags().BoolVar(&rootArgs.verbose, "verbose", false, "print generated objects")
configureDefaultNamespace()
kubeconfigArgs.APIServer = nil // prevent AddFlags from configuring --server flag
kubeconfigArgs.Timeout = nil // prevent AddFlags from configuring --request-timeout flag, we have --timeout instead
kubeconfigArgs.AddFlags(rootCmd.PersistentFlags())
@ -198,8 +201,10 @@ func main() {
}
}
func configureDefaultNamespace() {
*kubeconfigArgs.Namespace = rootArgs.defaults.Namespace
func GetDesiredNamespace(cfg *genericclioptions.ConfigFlags) string {
if *cfg.Namespace != "" {
return *cfg.Namespace
}
fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE")
if fromEnv != "" {
// namespace must be a valid DNS label. Assess against validation
@ -207,12 +212,29 @@ func configureDefaultNamespace() {
// may not be actively provided by end-user.
if e := validation.IsDNS1123Label(fromEnv); len(e) > 0 {
logger.Warningf(" ignoring invalid FLUX_SYSTEM_NAMESPACE: %q", fromEnv)
return
} else {
return fromEnv
}
}
kubeconfigArgs.Namespace = &fromEnv
if _, has := os.LookupEnv("FLUX_NS_FOLLOW_KUBECONTEXT"); has {
rawCfg, err := cfg.ToRawKubeConfigLoader().RawConfig()
if err != nil {
logger.Warningf(" failed parsing kubeconfig, ignoring: %q", fromEnv)
} else {
ctx := *cfg.Context
if ctx == "" {
ctx = rawCfg.CurrentContext
}
ns := rawCfg.Contexts[ctx].Namespace
if ns != "" {
return ns
}
}
}
return rootArgs.defaults.Namespace
}
// readPasswordFromStdin reads a password from stdin and returns the input
// with trailing newline and/or carriage return removed. It also makes sure that terminal

@ -85,7 +85,7 @@ func buildComponentObjectRefs(components ...string) ([]object.ObjMetadata, error
var objRefs []object.ObjMetadata
for _, deployment := range components {
objRefs = append(objRefs, object.ObjMetadata{
Namespace: *kubeconfigArgs.Namespace,
Namespace: GetDesiredNamespace(kubeconfigArgs),
Name: deployment,
GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"},
})

Loading…
Cancel
Save