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{ var repo = imagev1.ImageRepository{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: objectName, Name: objectName,
Namespace: *kubeconfigArgs.Namespace, Namespace: GetDesiredNamespace(kubeconfigArgs),
Labels: labels, Labels: labels,
}, },
Spec: imagev1.ImageRepositorySpec{ Spec: imagev1.ImageRepositorySpec{

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

@ -146,7 +146,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
opts := install.Options{ opts := install.Options{
BaseURL: installArgs.manifestsPath, BaseURL: installArgs.manifestsPath,
Version: installArgs.version, Version: installArgs.version,
Namespace: *kubeconfigArgs.Namespace, Namespace: GetDesiredNamespace(kubeconfigArgs),
Components: components, Components: components,
Registry: installArgs.registry, Registry: installArgs.registry,
ImagePullSecret: installArgs.imagePullSecret, ImagePullSecret: installArgs.imagePullSecret,
@ -181,7 +181,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
} }
logger.Successf("manifests build completed") 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)) applyOutput, err := utils.Apply(ctx, kubeconfigArgs, kubeclientOptions, tmpDir, filepath.Join(tmpDir, manifest.Path))
if err != nil { 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) return fmt.Errorf("error getting namespace: %w", err)
} }
if ns == "" {
return nil
}
if e := validation.IsDNS1123Label(ns); len(e) > 0 { if e := validation.IsDNS1123Label(ns); len(e) > 0 {
return fmt.Errorf("namespace must be a valid DNS label: %q", ns) 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().DurationVar(&rootArgs.timeout, "timeout", 5*time.Minute, "timeout for this operation")
rootCmd.PersistentFlags().BoolVar(&rootArgs.verbose, "verbose", false, "print generated objects") rootCmd.PersistentFlags().BoolVar(&rootArgs.verbose, "verbose", false, "print generated objects")
configureDefaultNamespace()
kubeconfigArgs.APIServer = nil // prevent AddFlags from configuring --server flag kubeconfigArgs.APIServer = nil // prevent AddFlags from configuring --server flag
kubeconfigArgs.Timeout = nil // prevent AddFlags from configuring --request-timeout flag, we have --timeout instead kubeconfigArgs.Timeout = nil // prevent AddFlags from configuring --request-timeout flag, we have --timeout instead
kubeconfigArgs.AddFlags(rootCmd.PersistentFlags()) kubeconfigArgs.AddFlags(rootCmd.PersistentFlags())
@ -198,8 +201,10 @@ func main() {
} }
} }
func configureDefaultNamespace() { func GetDesiredNamespace(cfg *genericclioptions.ConfigFlags) string {
*kubeconfigArgs.Namespace = rootArgs.defaults.Namespace if *cfg.Namespace != "" {
return *cfg.Namespace
}
fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE") fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE")
if fromEnv != "" { if fromEnv != "" {
// namespace must be a valid DNS label. Assess against validation // namespace must be a valid DNS label. Assess against validation
@ -207,11 +212,28 @@ func configureDefaultNamespace() {
// may not be actively provided by end-user. // may not be actively provided by end-user.
if e := validation.IsDNS1123Label(fromEnv); len(e) > 0 { if e := validation.IsDNS1123Label(fromEnv); len(e) > 0 {
logger.Warningf(" ignoring invalid FLUX_SYSTEM_NAMESPACE: %q", fromEnv) 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 // readPasswordFromStdin reads a password from stdin and returns the input

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

Loading…
Cancel
Save