-
Notifications
You must be signed in to change notification settings - Fork 244
/
deploy.go
108 lines (88 loc) · 3.83 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package deploy
import (
"errors"
"fmt"
"strings"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
devfilefs "github.com/devfile/library/pkg/testingutil/filesystem"
"k8s.io/klog"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/devfile/image"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/service"
)
type DeployClient struct {
kubeClient kclient.ClientInterface
}
var _ Client = (*DeployClient)(nil)
func NewDeployClient(kubeClient kclient.ClientInterface) *DeployClient {
return &DeployClient{
kubeClient: kubeClient,
}
}
func (o *DeployClient) Deploy(devfileObj parser.DevfileObj, path string, appName string) error {
deployHandler := newDeployHandler(devfileObj, path, o.kubeClient, appName)
return libdevfile.Deploy(devfileObj, deployHandler)
}
type deployHandler struct {
devfileObj parser.DevfileObj
path string
kubeClient kclient.ClientInterface
appName string
}
var _ libdevfile.Handler = (*deployHandler)(nil)
func newDeployHandler(devfileObj parser.DevfileObj, path string, kubeClient kclient.ClientInterface, appName string) *deployHandler {
return &deployHandler{
devfileObj: devfileObj,
path: path,
kubeClient: kubeClient,
appName: appName,
}
}
// ApplyImage builds and pushes the OCI image to be used on Kubernetes
func (o *deployHandler) ApplyImage(img v1alpha2.Component) error {
return image.BuildPushSpecificImage(o.path, img, true)
}
// ApplyKubernetes applies inline Kubernetes YAML from the devfile.yaml file
func (o *deployHandler) ApplyKubernetes(kubernetes v1alpha2.Component) error {
// Validate if the GVRs represented by Kubernetes inlined components are supported by the underlying cluster
_, err := service.ValidateResourceExist(o.kubeClient, o.devfileObj, kubernetes, o.path)
if err != nil {
return err
}
// Get the most common labels that's applicable to all resources being deployed.
// Set the mode to DEPLOY. Regardless of what Kubernetes resource we are deploying.
labels := odolabels.GetLabels(o.devfileObj.Data.GetMetadata().Name, o.appName, odolabels.ComponentDeployMode)
klog.V(4).Infof("Injecting labels: %+v into k8s artifact", labels)
// Create the annotations
// Retrieve the component type from the devfile and also inject it into the list of annotations
annotations := make(map[string]string)
odolabels.SetProjectType(annotations, component.GetComponentTypeFromDevfileMetadata(o.devfileObj.Data.GetMetadata()))
// Get the Kubernetes component
u, err := libdevfile.GetK8sComponentAsUnstructured(o.devfileObj, kubernetes.Name, o.path, devfilefs.DefaultFs{})
if err != nil {
return err
}
// Deploy the actual Kubernetes component and error out if there's an issue.
log.Sectionf("Deploying Kubernetes Component: %s", u.GetName())
isOperatorBackedService, err := service.PushKubernetesResource(o.kubeClient, u, labels, annotations)
if err != nil {
return fmt.Errorf("failed to create service(s) associated with the component: %w", err)
}
if isOperatorBackedService {
log.Successf("Kubernetes resource %q on the cluster; refer %q to know how to link it to the component", strings.Join([]string{u.GetKind(), u.GetName()}, "/"), "odo link -h")
}
return nil
}
// Execute will deploy the listed information in the `exec` section of devfile.yaml
// We currently do NOT support this in `odo deploy`.
func (o *deployHandler) Execute(command v1alpha2.Command) error {
// TODO:
// * Make sure we inject the "deploy" mode label once we implement exec in `odo deploy`
// * Make sure you inject the "component type" label once we implement exec.
return errors.New("exec command is not implemented for Deploy")
}