airshipctl/pkg/phase/executors/ephemeral.go
Ruslan Aliev 5a6f1096eb Groom phase/executors package
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
2021-01-12 16:31:43 -06:00

143 lines
3.8 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 (
"context"
"io"
"time"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/bootstrap/ephemeral"
"opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
)
var _ ifc.Executor = &EphemeralExecutor{}
// EphemeralExecutor contains resources for ephemeral executor
type EphemeralExecutor struct {
ExecutorBundle document.Bundle
ExecutorDocument document.Document
BootConf *v1alpha1.BootConfiguration
Container container.Container
}
// NewEphemeralExecutor creates instance of phase executor
func NewEphemeralExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
apiObj := &v1alpha1.BootConfiguration{}
err := cfg.ExecutorDocument.ToAPIObject(apiObj, v1alpha1.Scheme)
if err != nil {
return nil, err
}
return &EphemeralExecutor{
ExecutorDocument: cfg.ExecutorDocument,
BootConf: apiObj,
}, nil
}
// Run ephemeral as a phase runner
func (c *EphemeralExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
defer close(evtCh)
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapStart,
Message: "Processing Ephemeral cluster operation ...",
})
if opts.DryRun {
log.Print("Dryrun: bootstrap container command will be skipped")
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapDryRun,
})
return
}
if c.Container == nil {
ctx := context.Background()
builder, err := container.NewContainer(
ctx,
c.BootConf.BootstrapContainer.ContainerRuntime,
c.BootConf.BootstrapContainer.Image)
if err != nil {
handleError(evtCh, err)
return
}
c.Container = builder
}
bootstrapOpts := ephemeral.BootstrapContainerOptions{
Container: c.Container,
Cfg: c.BootConf,
Sleep: time.Sleep,
}
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapValidation,
Message: "Verifying executor manifest document ...",
})
err := bootstrapOpts.VerifyInputs()
if err != nil {
handleError(evtCh, err)
return
}
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapRun,
Message: "Creating and starting the Bootstrap Container ...",
})
err = bootstrapOpts.CreateBootstrapContainer()
if err != nil {
handleError(evtCh, err)
return
}
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapValidation,
Message: "Verifying generation of kubeconfig file ...",
})
err = bootstrapOpts.VerifyArtifacts()
if err != nil {
handleError(evtCh, err)
return
}
evtCh <- events.NewEvent().WithBootstrapEvent(events.BootstrapEvent{
Operation: events.BootstrapEnd,
Message: "Ephemeral cluster operation has completed successfully and artifacts verified",
})
}
// Validate executor configuration and documents
func (c *EphemeralExecutor) Validate() error {
return errors.ErrNotImplemented{}
}
// Render executor documents
func (c *EphemeralExecutor) Render(w io.Writer, _ ifc.RenderOptions) error {
log.Print("Ephemeral Executor Render() will be implemented later.")
return nil
}