5a6f1096eb
All the duplicated methods were removed. Unit tests reorganized. Test coverage increased. Change-Id: I0f7bd3eea63c101195ea50c0369e62945d73f297 Signed-off-by: Ruslan Aliev <raliev@mirantis.com> Closes: #432
139 lines
4.2 KiB
Go
139 lines
4.2 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 executors
|
|
|
|
import (
|
|
"io"
|
|
"time"
|
|
|
|
"sigs.k8s.io/cli-utils/pkg/common"
|
|
|
|
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
|
"opendev.org/airship/airshipctl/pkg/cluster/clustermap"
|
|
"opendev.org/airship/airshipctl/pkg/document"
|
|
"opendev.org/airship/airshipctl/pkg/errors"
|
|
"opendev.org/airship/airshipctl/pkg/events"
|
|
k8sapplier "opendev.org/airship/airshipctl/pkg/k8s/applier"
|
|
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
|
|
"opendev.org/airship/airshipctl/pkg/k8s/utils"
|
|
"opendev.org/airship/airshipctl/pkg/log"
|
|
"opendev.org/airship/airshipctl/pkg/phase/ifc"
|
|
)
|
|
|
|
var _ ifc.Executor = &KubeApplierExecutor{}
|
|
|
|
// KubeApplierExecutor applies resources to kubernetes
|
|
type KubeApplierExecutor struct {
|
|
ExecutorBundle document.Bundle
|
|
ExecutorDocument document.Document
|
|
BundleName string
|
|
Helper ifc.Helper
|
|
|
|
apiObject *airshipv1.KubernetesApply
|
|
cleanup kubeconfig.Cleanup
|
|
clusterMap clustermap.ClusterMap
|
|
clusterName string
|
|
kubeconfig kubeconfig.Interface
|
|
}
|
|
|
|
// NewKubeApplierExecutor returns instance of executor
|
|
func NewKubeApplierExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
|
|
apiObj := &airshipv1.KubernetesApply{}
|
|
err := cfg.ExecutorDocument.ToAPIObject(apiObj, airshipv1.Scheme)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
bundle, err := cfg.BundleFactory()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &KubeApplierExecutor{
|
|
ExecutorBundle: bundle,
|
|
BundleName: cfg.PhaseName,
|
|
Helper: cfg.Helper,
|
|
ExecutorDocument: cfg.ExecutorDocument,
|
|
apiObject: apiObj,
|
|
clusterMap: cfg.ClusterMap,
|
|
clusterName: cfg.ClusterName,
|
|
kubeconfig: cfg.KubeConfig,
|
|
}, nil
|
|
}
|
|
|
|
// Run executor, should be performed in separate go routine
|
|
func (e *KubeApplierExecutor) Run(ch chan events.Event, runOpts ifc.RunOptions) {
|
|
applier, filteredBundle, err := e.prepareApplier(ch)
|
|
if err != nil {
|
|
handleError(ch, err)
|
|
close(ch)
|
|
return
|
|
}
|
|
defer e.cleanup()
|
|
dryRunStrategy := common.DryRunNone
|
|
if runOpts.DryRun {
|
|
dryRunStrategy = common.DryRunClient
|
|
}
|
|
timeout := time.Second * time.Duration(e.apiObject.Config.WaitOptions.Timeout)
|
|
if int64(runOpts.Timeout/time.Second) != 0 {
|
|
timeout = runOpts.Timeout
|
|
}
|
|
|
|
log.Debugf("WaitTimeout: %v", timeout)
|
|
applyOptions := k8sapplier.ApplyOptions{
|
|
DryRunStrategy: dryRunStrategy,
|
|
Prune: e.apiObject.Config.PruneOptions.Prune,
|
|
BundleName: e.BundleName,
|
|
WaitTimeout: timeout,
|
|
}
|
|
applier.ApplyBundle(filteredBundle, applyOptions)
|
|
}
|
|
|
|
func (e *KubeApplierExecutor) prepareApplier(ch chan events.Event) (*k8sapplier.Applier, document.Bundle, error) {
|
|
log.Debug("Getting kubeconfig context name from cluster map")
|
|
context, err := e.clusterMap.ClusterKubeconfigContext(e.clusterName)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
log.Debug("Getting kubeconfig file information from kubeconfig provider")
|
|
path, cleanup, err := e.kubeconfig.GetFile()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
log.Debug("Filtering out documents that shouldn't be applied to kubernetes from document bundle")
|
|
bundle, err := e.ExecutorBundle.SelectBundle(document.NewDeployToK8sSelector())
|
|
if err != nil {
|
|
cleanup()
|
|
return nil, nil, err
|
|
}
|
|
// set up cleanup only if all calls up to here were successful
|
|
e.cleanup = cleanup
|
|
log.Debugf("Using kubeconfig at '%s' and context '%s'", path, context)
|
|
factory := utils.FactoryFromKubeConfig(path, context)
|
|
return k8sapplier.NewApplier(ch, factory), bundle, nil
|
|
}
|
|
|
|
// Validate document set
|
|
func (e *KubeApplierExecutor) Validate() error {
|
|
return errors.ErrNotImplemented{}
|
|
}
|
|
|
|
// Render document set
|
|
func (e *KubeApplierExecutor) Render(w io.Writer, o ifc.RenderOptions) error {
|
|
bundle, err := e.ExecutorBundle.SelectBundle(o.FilterSelector)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return bundle.Write(w)
|
|
}
|