
In order to widely use filesystem interface in airshipctl there is need to move it to separate package to avoid importing unnecessary dependencies from document package and, as a consequence, possible cyclic dependencies. * filesystem moved from document/fs to pkg/fs Change-Id: I3b6298462f03db43594a9fa26bf23ab7687c5589 Signed-off-by: Ruslan Aliev <raliev@mirantis.com> Relates-To: #415
105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
/*
|
|
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
|
|
|
|
https://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.
|
|
*/
|
|
|
|
package kubeconfig
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/cluster/clustermap"
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
"opendev.org/airship/airshipctl/pkg/errors"
|
|
"opendev.org/airship/airshipctl/pkg/fs"
|
|
"opendev.org/airship/airshipctl/pkg/util"
|
|
)
|
|
|
|
// KubeconfigDefaultFileName is a default name for kubeconfig
|
|
const KubeconfigDefaultFileName = "kubeconfig"
|
|
|
|
// NewBuilder returns instance of kubeconfig builder.
|
|
func NewBuilder() *Builder {
|
|
return &Builder{}
|
|
}
|
|
|
|
// Builder is an object that allows to build a kubeconfig based on various provided sources
|
|
// such as path to kubeconfig, path to bundle that should contain kubeconfig and parent cluster
|
|
type Builder struct {
|
|
path string
|
|
bundlePath string
|
|
clusterName string
|
|
root string
|
|
|
|
clusterMap clustermap.ClusterMap
|
|
}
|
|
|
|
// WithPath allows to set path to prexisting kubeconfig
|
|
func (b *Builder) WithPath(filePath string) *Builder {
|
|
b.path = filePath
|
|
return b
|
|
}
|
|
|
|
// WithBundle allows to set path to bundle that should contain kubeconfig api object
|
|
func (b *Builder) WithBundle(bundlePath string) *Builder {
|
|
b.bundlePath = bundlePath
|
|
return b
|
|
}
|
|
|
|
// WithClusterMap allows to set a parent cluster, that can be used to extract kubeconfig for target cluster
|
|
func (b *Builder) WithClusterMap(cMap clustermap.ClusterMap) *Builder {
|
|
b.clusterMap = cMap
|
|
return b
|
|
}
|
|
|
|
// WithClusterName allows to reach to a cluster to download kubeconfig from there
|
|
func (b *Builder) WithClusterName(clusterName string) *Builder {
|
|
b.clusterName = clusterName
|
|
return b
|
|
}
|
|
|
|
// WithTempRoot allows to set temp root for kubeconfig
|
|
func (b *Builder) WithTempRoot(root string) *Builder {
|
|
b.root = root
|
|
return b
|
|
}
|
|
|
|
// Build builds a kubeconfig interface to be used
|
|
func (b *Builder) Build() Interface {
|
|
switch {
|
|
case b.path != "":
|
|
fSys := fs.NewDocumentFs()
|
|
return NewKubeConfig(FromFile(b.path, fSys), InjectFilePath(b.path, fSys), InjectTempRoot(b.root))
|
|
case b.fromParent():
|
|
// TODO add method that would get kubeconfig from parent cluster and glue it together
|
|
// with parent kubeconfig if needed
|
|
return NewKubeConfig(func() ([]byte, error) {
|
|
return nil, errors.ErrNotImplemented{}
|
|
})
|
|
case b.bundlePath != "":
|
|
return NewKubeConfig(FromBundle(b.bundlePath), InjectTempRoot(b.root))
|
|
default:
|
|
fSys := fs.NewDocumentFs()
|
|
// return default path to kubeconfig file in airship workdir
|
|
path := filepath.Join(util.UserHomeDir(), config.AirshipConfigDir, KubeconfigDefaultFileName)
|
|
return NewKubeConfig(FromFile(path, fSys), InjectFilePath(path, fSys), InjectTempRoot(b.root))
|
|
}
|
|
}
|
|
|
|
// fromParent checks if we should get kubeconfig from parent cluster secret
|
|
func (b *Builder) fromParent() bool {
|
|
if b.clusterMap == nil {
|
|
return false
|
|
}
|
|
return b.clusterMap.DynamicKubeConfig(b.clusterName)
|
|
}
|