forked from redhat-developer/odo
/
occlient.go
3077 lines (2650 loc) · 108 KB
/
occlient.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package occlient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/preference"
"github.com/openshift/odo/pkg/util"
// api clientsets
servicecatalogclienset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/typed/servicecatalog/v1beta1"
appsschema "github.com/openshift/client-go/apps/clientset/versioned/scheme"
appsclientset "github.com/openshift/client-go/apps/clientset/versioned/typed/apps/v1"
buildschema "github.com/openshift/client-go/build/clientset/versioned/scheme"
buildclientset "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
imageclientset "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
projectclientset "github.com/openshift/client-go/project/clientset/versioned/typed/project/v1"
routeclientset "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
userclientset "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
// api resource types
scv1beta1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
appsv1 "github.com/openshift/api/apps/v1"
buildv1 "github.com/openshift/api/build/v1"
dockerapiv10 "github.com/openshift/api/image/docker10"
imagev1 "github.com/openshift/api/image/v1"
projectv1 "github.com/openshift/api/project/v1"
routev1 "github.com/openshift/api/route/v1"
oauthv1client "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/version"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/client-go/util/retry"
)
var (
DEPLOYMENT_CONFIG_NOT_FOUND_ERROR_STR string = "deploymentconfigs.apps.openshift.io \"%s\" not found"
DEPLOYMENT_CONFIG_NOT_FOUND error = fmt.Errorf("Requested deployment config does not exist")
)
// CreateArgs is a container of attributes of component create action
type CreateArgs struct {
Name string
SourcePath string
SourceRef string
SourceType config.SrcType
ImageName string
EnvVars []string
Ports []string
Resources *corev1.ResourceRequirements
ApplicationName string
Wait bool
// StorageToBeMounted describes the storage to be created
// storagePath is the key of the map, the generatedPVC is the value of the map
StorageToBeMounted map[string]*corev1.PersistentVolumeClaim
StdOut io.Writer
}
const (
OcUpdateTimeout = 5 * time.Minute
OcBuildTimeout = 5 * time.Minute
OpenShiftNameSpace = "openshift"
// The length of the string to be generated for names of resources
nameLength = 5
// Default Image that will be used containing the supervisord binary and assembly scripts
// use getBoostrapperImage() function instead of this variable
defaultBootstrapperImage = "registry.access.redhat.com/openshiftdo/odo-init-image-rhel7:1.0.2"
// ENV variable to overwrite image used to bootstrap SupervisorD in S2I builder Image
bootstrapperImageEnvName = "ODO_BOOTSTRAPPER_IMAGE"
// Create a custom name and (hope) that users don't use the *exact* same name in their deployment
supervisordVolumeName = "odo-supervisord-shared-data"
// ComponentPortAnnotationName annotation is used on the secrets that are created for each exposed port of the component
ComponentPortAnnotationName = "component-port"
// EnvS2IScriptsURL is an env var exposed to https://github.com/openshift/odo-init-image/blob/master/assemble-and-restart to indicate location of s2i scripts in this case assemble script
EnvS2IScriptsURL = "ODO_S2I_SCRIPTS_URL"
// EnvS2IScriptsProtocol is an env var exposed to https://github.com/openshift/odo-init-image/blob/master/assemble-and-restart to indicate the way to access location of s2i scripts indicated by ${${EnvS2IScriptsURL}} above
EnvS2IScriptsProtocol = "ODO_S2I_SCRIPTS_PROTOCOL"
// EnvS2ISrcOrBinPath is an env var exposed by s2i to indicate where the builder image expects the component source or binary to reside
EnvS2ISrcOrBinPath = "ODO_S2I_SRC_BIN_PATH"
// EnvS2ISrcBackupDir is the env var that points to the directory that holds a backup of component source
// This is required bcoz, s2i assemble script moves(hence deletes contents) the contents of $ODO_S2I_SRC_BIN_PATH to $APP_ROOT during which $APP_DIR alo needs to be empty so that mv doesn't complain pushing to an already exisiting dir with same name
EnvS2ISrcBackupDir = "ODO_SRC_BACKUP_DIR"
// S2IScriptsURLLabel S2I script location Label name
// Ref: https://docs.openshift.com/enterprise/3.2/creating_images/s2i.html#build-process
S2IScriptsURLLabel = "io.openshift.s2i.scripts-url"
// S2IBuilderImageName is the S2I builder image name
S2IBuilderImageName = "name"
// S2ISrcOrBinLabel is the label that provides, path where S2I expects component source or binary
S2ISrcOrBinLabel = "io.openshift.s2i.destination"
// EnvS2IBuilderImageName is the label that provides the name of builder image in component
EnvS2IBuilderImageName = "ODO_S2I_BUILDER_IMG"
// EnvS2IDeploymentDir is an env var exposed to https://github.com/openshift/odo-init-image/blob/master/assemble-and-restart to indicate s2i deployment directory
EnvS2IDeploymentDir = "ODO_S2I_DEPLOYMENT_DIR"
// DefaultS2ISrcOrBinPath is the default path where S2I expects source/binary artifacts in absence of $S2ISrcOrBinLabel in builder image
// Ref: https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#required-image-contents
DefaultS2ISrcOrBinPath = "/tmp"
// DefaultS2ISrcBackupDir is the default path where odo backs up the component source
DefaultS2ISrcBackupDir = "/opt/app-root/src-backup"
// EnvS2IWorkingDir is an env var to odo-init-image assemble-and-restart.sh to indicate to it the s2i working directory
EnvS2IWorkingDir = "ODO_S2I_WORKING_DIR"
DefaultAppRootDir = "/opt/app-root"
)
// S2IPaths is a struct that will hold path to S2I scripts and the protocol indicating access to them, component source/binary paths, artifacts deployments directory
// These are passed as env vars to component pod
type S2IPaths struct {
ScriptsPathProtocol string
ScriptsPath string
SrcOrBinPath string
DeploymentDir string
WorkingDir string
SrcBackupPath string
BuilderImgName string
}
// UpdateComponentParams serves the purpose of holding the arguments to a component update request
type UpdateComponentParams struct {
// CommonObjectMeta is the object meta containing the labels and annotations expected for the new deployment
CommonObjectMeta metav1.ObjectMeta
// ResourceLimits are the cpu and memory constraints to be applied on to the component
ResourceLimits corev1.ResourceRequirements
// EnvVars to be exposed
EnvVars []corev1.EnvVar
// ExistingDC is the dc of the existing component that is requested for an update
ExistingDC *appsv1.DeploymentConfig
// DcRollOutWaitCond holds the logic to wait for dc with requested updates to be applied
DcRollOutWaitCond dcRollOutWait
// ImageMeta describes the image to be used in dc(builder image for local/binary and built component image for git deployments)
ImageMeta CommonImageMeta
// StorageToBeMounted describes the storage to be mounted
// storagePath is the key of the map, the generatedPVC is the value of the map
StorageToBeMounted map[string]*corev1.PersistentVolumeClaim
// StorageToBeUnMounted describes the storage to be unmounted
// path is the key of the map,storageName is the value of the map
StorageToBeUnMounted map[string]string
}
// S2IDeploymentsDir is a set of possible S2I labels that provides S2I deployments directory
// This label is not uniform across different builder images. This slice is expected to grow as odo adds support to more component types and/or the respective builder images use different labels
var S2IDeploymentsDir = []string{
"com.redhat.deployments-dir",
"org.jboss.deployments-dir",
"org.jboss.container.deployments-dir",
}
// errorMsg is the message for user when invalid configuration error occurs
const errorMsg = `
Please login to your server:
odo login https://mycluster.mydomain.com
`
type Client struct {
kubeClient kubernetes.Interface
imageClient imageclientset.ImageV1Interface
appsClient appsclientset.AppsV1Interface
buildClient buildclientset.BuildV1Interface
projectClient projectclientset.ProjectV1Interface
serviceCatalogClient servicecatalogclienset.ServicecatalogV1beta1Interface
routeClient routeclientset.RouteV1Interface
userClient userclientset.UserV1Interface
KubeConfig clientcmd.ClientConfig
Namespace string
}
func getBootstrapperImage() string {
if env, ok := os.LookupEnv(bootstrapperImageEnvName); ok {
return env
}
return defaultBootstrapperImage
}
// New creates a new client
func New() (*Client, error) {
var client Client
// initialize client-go clients
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
client.KubeConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := client.KubeConfig.ClientConfig()
if err != nil {
return nil, errors.New(err.Error() + errorMsg)
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
client.kubeClient = kubeClient
imageClient, err := imageclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.imageClient = imageClient
appsClient, err := appsclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.appsClient = appsClient
buildClient, err := buildclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.buildClient = buildClient
serviceCatalogClient, err := servicecatalogclienset.NewForConfig(config)
if err != nil {
return nil, err
}
client.serviceCatalogClient = serviceCatalogClient
projectClient, err := projectclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.projectClient = projectClient
routeClient, err := routeclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.routeClient = routeClient
userClient, err := userclientset.NewForConfig(config)
if err != nil {
return nil, err
}
client.userClient = userClient
namespace, _, err := client.KubeConfig.Namespace()
if err != nil {
return nil, err
}
client.Namespace = namespace
return &client, nil
}
// ParseImageName parse image reference
// returns (imageNamespace, imageName, tag, digest, error)
// if image is referenced by tag (name:tag) than digest is ""
// if image is referenced by digest (name@digest) than tag is ""
func ParseImageName(image string) (string, string, string, string, error) {
digestParts := strings.Split(image, "@")
if len(digestParts) == 2 {
// image is references digest
// Safe path image name and digest are non empty, else error
if digestParts[0] != "" && digestParts[1] != "" {
// Image name might be fully qualified name of form: Namespace/ImageName
imangeNameParts := strings.Split(digestParts[0], "/")
if len(imangeNameParts) == 2 {
return imangeNameParts[0], imangeNameParts[1], "", digestParts[1], nil
}
return "", imangeNameParts[0], "", digestParts[1], nil
}
} else if len(digestParts) == 1 && digestParts[0] != "" { // Filter out empty image name
tagParts := strings.Split(image, ":")
if len(tagParts) == 2 {
// ":1.0.0 is invalid image name"
if tagParts[0] != "" {
// Image name might be fully qualified name of form: Namespace/ImageName
imangeNameParts := strings.Split(tagParts[0], "/")
if len(imangeNameParts) == 2 {
return imangeNameParts[0], imangeNameParts[1], tagParts[1], "", nil
}
return "", tagParts[0], tagParts[1], "", nil
}
} else if len(tagParts) == 1 {
// Image name might be fully qualified name of form: Namespace/ImageName
imangeNameParts := strings.Split(tagParts[0], "/")
if len(imangeNameParts) == 2 {
return imangeNameParts[0], imangeNameParts[1], "latest", "", nil
}
return "", tagParts[0], "latest", "", nil
}
}
return "", "", "", "", fmt.Errorf("invalid image reference %s", image)
}
// imageWithMetadata mutates the given image. It parses raw DockerImageManifest data stored in the image and
// fills its DockerImageMetadata and other fields.
// Copied from v3.7 github.com/openshift/origin/pkg/image/apis/image/v1/helpers.go
func imageWithMetadata(image *imagev1.Image) error {
// Check if the metadata are already filled in for this image.
meta, hasMetadata := image.DockerImageMetadata.Object.(*dockerapiv10.DockerImage)
if hasMetadata && meta.Size > 0 {
return nil
}
version := image.DockerImageMetadataVersion
if len(version) == 0 {
version = "1.0"
}
obj := &dockerapiv10.DockerImage{}
if len(image.DockerImageMetadata.Raw) != 0 {
if err := json.Unmarshal(image.DockerImageMetadata.Raw, obj); err != nil {
return err
}
image.DockerImageMetadata.Object = obj
}
image.DockerImageMetadataVersion = version
return nil
}
// GetPortsFromBuilderImage returns list of available port from given builder image of given component type
func (c *Client) GetPortsFromBuilderImage(componentType string) ([]string, error) {
// checking port through builder image
imageNS, imageName, imageTag, _, err := ParseImageName(componentType)
if err != nil {
return []string{}, err
}
imageStream, err := c.GetImageStream(imageNS, imageName, imageTag)
if err != nil {
return []string{}, err
}
imageStreamImage, err := c.GetImageStreamImage(imageStream, imageTag)
if err != nil {
return []string{}, err
}
containerPorts, err := c.GetExposedPorts(imageStreamImage)
if err != nil {
return []string{}, err
}
var portList []string
for _, po := range containerPorts {
port := fmt.Sprint(po.ContainerPort) + "/" + string(po.Protocol)
portList = append(portList, port)
}
if len(portList) == 0 {
return []string{}, fmt.Errorf("given component type doesn't expose any ports, please use --port flag to specify a port")
}
return portList, nil
}
// RunLogout logs out the current user from cluster
func (c *Client) RunLogout(stdout io.Writer) error {
output, err := c.userClient.Users().Get("~", metav1.GetOptions{})
if err != nil {
glog.V(1).Infof("%v : unable to get userinfo", err)
}
// read the current config form ~/.kube/config
conf, err := c.KubeConfig.ClientConfig()
if err != nil {
glog.V(1).Infof("%v : unable to get client config", err)
}
// initialising oauthv1client
client, err := oauthv1client.NewForConfig(conf)
if err != nil {
glog.V(1).Infof("%v : unable to create a new OauthV1Client", err)
}
// deleting token form the server
if err := client.OAuthAccessTokens().Delete(conf.BearerToken, &metav1.DeleteOptions{}); err != nil {
glog.V(1).Infof("%v", err)
}
rawConfig, err := c.KubeConfig.RawConfig()
if err != nil {
glog.V(1).Infof("%v : unable to switch to project", err)
}
// deleting token for the current server from local config
for key, value := range rawConfig.AuthInfos {
if key == rawConfig.Contexts[rawConfig.CurrentContext].AuthInfo {
value.Token = ""
}
}
err = clientcmd.ModifyConfig(clientcmd.NewDefaultClientConfigLoadingRules(), rawConfig, true)
if err != nil {
glog.V(1).Infof("%v : unable to write config to config file", err)
}
_, err = io.WriteString(stdout, fmt.Sprintf("Logged \"%v\" out on \"%v\"\n", output.Name, conf.Host))
return err
}
// isServerUp returns true if server is up and running
// server parameter has to be a valid url
func isServerUp(server string) bool {
// initialising the default timeout, this will be used
// when the value is not readable from config
ocRequestTimeout := preference.DefaultTimeout * time.Second
// checking the value of timeout in config
// before proceeding with default timeout
cfg, configReadErr := preference.New()
if configReadErr != nil {
glog.V(4).Info(errors.Wrap(configReadErr, "unable to read config file"))
} else {
ocRequestTimeout = time.Duration(cfg.GetTimeout()) * time.Second
}
address, err := util.GetHostWithPort(server)
if err != nil {
glog.V(4).Infof("Unable to parse url %s (%s)", server, err)
}
glog.V(4).Infof("Trying to connect to server %s", address)
_, connectionError := net.DialTimeout("tcp", address, time.Duration(ocRequestTimeout))
if connectionError != nil {
glog.V(4).Info(errors.Wrap(connectionError, "unable to connect to server"))
return false
}
glog.V(4).Infof("Server %v is up", server)
return true
}
func (c *Client) GetCurrentProjectName() string {
return c.Namespace
}
// GetProjectNames return list of existing projects that user has access to.
func (c *Client) GetProjectNames() ([]string, error) {
projects, err := c.projectClient.Projects().List(metav1.ListOptions{})
if err != nil {
return nil, errors.Wrap(err, "unable to list projects")
}
var projectNames []string
for _, p := range projects.Items {
projectNames = append(projectNames, p.Name)
}
return projectNames, nil
}
// GetProject returns project based on the name of the project.Errors related to
// project not being found or forbidden are translated to nil project for compatibility
func (c *Client) GetProject(projectName string) (*projectv1.Project, error) {
prj, err := c.projectClient.Projects().Get(projectName, metav1.GetOptions{})
if err != nil {
istatus, ok := err.(kerrors.APIStatus)
if ok {
status := istatus.Status()
if status.Reason == metav1.StatusReasonNotFound || status.Reason == metav1.StatusReasonForbidden {
return nil, nil
}
} else {
return nil, err
}
}
return prj, err
}
// CreateNewProject creates project with given projectName
func (c *Client) CreateNewProject(projectName string, wait bool) error {
// Instantiate watcher before requesting new project
// If watched is created after the project it can lead to situation when the project is created before the watcher.
// When this happens, it gets stuck waiting for event that already happened.
var watcher watch.Interface
var err error
if wait {
watcher, err = c.projectClient.Projects().Watch(metav1.ListOptions{
FieldSelector: fields.Set{"metadata.name": projectName}.AsSelector().String(),
})
if err != nil {
return errors.Wrapf(err, "unable to watch new project %s creation", projectName)
}
defer watcher.Stop()
}
projectRequest := &projectv1.ProjectRequest{
ObjectMeta: metav1.ObjectMeta{
Name: projectName,
},
}
_, err = c.projectClient.ProjectRequests().Create(projectRequest)
if err != nil {
return errors.Wrapf(err, "unable to create new project %s", projectName)
}
if watcher != nil {
for {
val, ok := <-watcher.ResultChan()
if !ok {
break
}
if e, ok := val.Object.(*projectv1.Project); ok {
glog.V(4).Infof("Project %s now exists", e.Name)
return nil
}
}
}
return nil
}
// SetCurrentProject sets the given projectName to current project
func (c *Client) SetCurrentProject(projectName string) error {
rawConfig, err := c.KubeConfig.RawConfig()
if err != nil {
return errors.Wrapf(err, "unable to switch to %s project", projectName)
}
rawConfig.Contexts[rawConfig.CurrentContext].Namespace = projectName
err = clientcmd.ModifyConfig(clientcmd.NewDefaultClientConfigLoadingRules(), rawConfig, true)
if err != nil {
return errors.Wrapf(err, "unable to switch to %s project", projectName)
}
// we set the current namespace to the current project as well
c.Namespace = projectName
return nil
}
// addLabelsToArgs adds labels from map to args as a new argument in format that oc requires
// --labels label1=value1,label2=value2
func addLabelsToArgs(labels map[string]string, args []string) []string {
if labels != nil {
var labelsString []string
for key, value := range labels {
labelsString = append(labelsString, fmt.Sprintf("%s=%s", key, value))
}
args = append(args, "--labels")
args = append(args, strings.Join(labelsString, ","))
}
return args
}
// getExposedPortsFromISI parse ImageStreamImage definition and return all exposed ports in form of ContainerPorts structs
func getExposedPortsFromISI(image *imagev1.ImageStreamImage) ([]corev1.ContainerPort, error) {
// file DockerImageMetadata
err := imageWithMetadata(&image.Image)
if err != nil {
return nil, err
}
var ports []corev1.ContainerPort
for exposedPort := range image.Image.DockerImageMetadata.Object.(*dockerapiv10.DockerImage).ContainerConfig.ExposedPorts {
splits := strings.Split(exposedPort, "/")
if len(splits) != 2 {
return nil, fmt.Errorf("invalid port %s", exposedPort)
}
portNumberI64, err := strconv.ParseInt(splits[0], 10, 32)
if err != nil {
return nil, errors.Wrapf(err, "invalid port number %s", splits[0])
}
portNumber := int32(portNumberI64)
var portProto corev1.Protocol
switch strings.ToUpper(splits[1]) {
case "TCP":
portProto = corev1.ProtocolTCP
case "UDP":
portProto = corev1.ProtocolUDP
default:
return nil, fmt.Errorf("invalid port protocol %s", splits[1])
}
port := corev1.ContainerPort{
Name: fmt.Sprintf("%d-%s", portNumber, strings.ToLower(string(portProto))),
ContainerPort: portNumber,
Protocol: portProto,
}
ports = append(ports, port)
}
return ports, nil
}
// GetImageStreams returns the Image Stream objects in the given namespace
func (c *Client) GetImageStreams(namespace string) ([]imagev1.ImageStream, error) {
imageStreamList, err := c.imageClient.ImageStreams(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, errors.Wrap(err, "unable to list imagestreams")
}
return imageStreamList.Items, nil
}
// GetImageStreamsNames returns the names of the image streams in a given
// namespace
func (c *Client) GetImageStreamsNames(namespace string) ([]string, error) {
imageStreams, err := c.GetImageStreams(namespace)
if err != nil {
return nil, errors.Wrap(err, "unable to get image streams")
}
var names []string
for _, imageStream := range imageStreams {
names = append(names, imageStream.Name)
}
return names, nil
}
// isTagInImageStream takes a imagestream and a tag and checks if the tag is present in the imagestream's status attribute
func isTagInImageStream(is imagev1.ImageStream, imageTag string) bool {
// Loop through the tags in the imagestream's status attribute
for _, tag := range is.Status.Tags {
// look for a matching tag
if tag.Tag == imageTag {
// Return true if found
return true
}
}
// Return false if not found.
return false
}
// GetImageStream returns the imagestream using image details like imageNS, imageName and imageTag
// imageNS can be empty in which case, this function searches currentNamespace on priority. If
// imagestream of required tag not found in current namespace, then searches openshift namespace.
// If not found, error out. If imageNS is not empty string, then, the requested imageNS only is searched
// for requested imagestream
func (c *Client) GetImageStream(imageNS string, imageName string, imageTag string) (*imagev1.ImageStream, error) {
var err error
var imageStream *imagev1.ImageStream
currentProjectName := c.GetCurrentProjectName()
/*
If User has not chosen image NS then,
1. Use image from current NS if available
2. If not 1, use default openshift NS
3. If not 2, return errors from both 1 and 2
else
Use user chosen namespace
If image doesn't exist in user chosen namespace,
error out
else
Proceed
*/
// User has not passed any particular ImageStream
if imageNS == "" {
// First try finding imagestream from current namespace
currentNSImageStream, e := c.imageClient.ImageStreams(currentProjectName).Get(imageName, metav1.GetOptions{})
if e != nil {
err = errors.Wrapf(e, "no match found for : %s in namespace %s", imageName, currentProjectName)
} else {
if isTagInImageStream(*currentNSImageStream, imageTag) {
return currentNSImageStream, nil
}
}
// If not in current namespace, try finding imagestream from openshift namespace
openshiftNSImageStream, e := c.imageClient.ImageStreams(OpenShiftNameSpace).Get(imageName, metav1.GetOptions{})
if e != nil {
// The image is not available in current Namespace.
err = errors.Wrapf(e, "no match found for : %s in namespace %s", imageName, OpenShiftNameSpace)
} else {
if isTagInImageStream(*openshiftNSImageStream, imageTag) {
return openshiftNSImageStream, nil
}
}
if e != nil && err != nil {
// Imagestream not found in openshift and current namespaces
return nil, err
}
// Required tag not in openshift and current namespaces
return nil, fmt.Errorf("image stream %s with tag %s not found in openshift and %s namespaces", imageName, imageTag, currentProjectName)
}
// Fetch imagestream from requested namespace
imageStream, err = c.imageClient.ImageStreams(imageNS).Get(imageName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(
err, "no match found for %s in namespace %s", imageName, imageNS,
)
}
if !isTagInImageStream(*imageStream, imageTag) {
return nil, fmt.Errorf("image stream %s with tag %s not found in %s namespaces", imageName, imageTag, currentProjectName)
}
return imageStream, nil
}
// GetSecret returns the Secret object in the given namespace
func (c *Client) GetSecret(name, namespace string) (*corev1.Secret, error) {
secret, err := c.kubeClient.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "unable to get the secret %s", secret)
}
return secret, nil
}
// GetImageStreamImage returns image and error if any, corresponding to the passed imagestream and image tag
func (c *Client) GetImageStreamImage(imageStream *imagev1.ImageStream, imageTag string) (*imagev1.ImageStreamImage, error) {
imageNS := imageStream.ObjectMeta.Namespace
imageName := imageStream.ObjectMeta.Name
for _, tag := range imageStream.Status.Tags {
// look for matching tag
if tag.Tag == imageTag {
glog.V(4).Infof("Found exact image tag match for %s:%s", imageName, imageTag)
if len(tag.Items) > 0 {
tagDigest := tag.Items[0].Image
imageStreamImageName := fmt.Sprintf("%s@%s", imageName, tagDigest)
// look for imageStreamImage for given tag (reference by digest)
imageStreamImage, err := c.imageClient.ImageStreamImages(imageNS).Get(imageStreamImageName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "unable to find ImageStreamImage with %s digest", imageStreamImageName)
}
return imageStreamImage, nil
}
return nil, fmt.Errorf("unable to find tag %s for image %s", imageTag, imageName)
}
}
// return error since its an unhandled case if code reaches here
return nil, fmt.Errorf("unable to find tag %s for image %s", imageTag, imageName)
}
// GetImageStreamTags returns all the ImageStreamTag objects in the given namespace
func (c *Client) GetImageStreamTags(namespace string) ([]imagev1.ImageStreamTag, error) {
imageStreamTagList, err := c.imageClient.ImageStreamTags(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, errors.Wrap(err, "unable to list imagestreamtags")
}
return imageStreamTagList.Items, nil
}
// GetExposedPorts returns list of ContainerPorts that are exposed by given image
func (c *Client) GetExposedPorts(imageStreamImage *imagev1.ImageStreamImage) ([]corev1.ContainerPort, error) {
var containerPorts []corev1.ContainerPort
// get ports that are exported by image
containerPorts, err := getExposedPortsFromISI(imageStreamImage)
if err != nil {
return nil, errors.Wrapf(err, "unable to get exported ports from image %+v", imageStreamImage)
}
return containerPorts, nil
}
func getAppRootVolumeName(dcName string) string {
return fmt.Sprintf("%s-s2idata", dcName)
}
// NewAppS2I is only used with "Git" as we need Build
// gitURL is the url of the git repo
// inputPorts is the array containing the string port values
// envVars is the array containing the string env var values
func (c *Client) NewAppS2I(params CreateArgs, commonObjectMeta metav1.ObjectMeta) error {
glog.V(4).Infof("Using BuilderImage: %s", params.ImageName)
imageNS, imageName, imageTag, _, err := ParseImageName(params.ImageName)
if err != nil {
return errors.Wrap(err, "unable to parse image name")
}
imageStream, err := c.GetImageStream(imageNS, imageName, imageTag)
if err != nil {
return errors.Wrap(err, "unable to retrieve ImageStream for NewAppS2I")
}
/*
Set imageNS to the commonObjectMeta.Namespace of above fetched imagestream because, the commonObjectMeta.Namespace passed here can potentially be emptystring
in which case, GetImageStream function resolves to correct commonObjectMeta.Namespace in accordance with priorities in GetImageStream
*/
imageNS = imageStream.ObjectMeta.Namespace
glog.V(4).Infof("Using imageNS: %s", imageNS)
imageStreamImage, err := c.GetImageStreamImage(imageStream, imageTag)
if err != nil {
return errors.Wrapf(err, "unable to create s2i app for %s", commonObjectMeta.Name)
}
var containerPorts []corev1.ContainerPort
if len(params.Ports) == 0 {
containerPorts, err = c.GetExposedPorts(imageStreamImage)
if err != nil {
return errors.Wrapf(err, "unable to get exposed ports for %s:%s", imageName, imageTag)
}
} else {
if err != nil {
return errors.Wrapf(err, "unable to create s2i app for %s", commonObjectMeta.Name)
}
containerPorts, err = util.GetContainerPortsFromStrings(params.Ports)
if err != nil {
return errors.Wrapf(err, "unable to get container ports from %v", params.Ports)
}
}
inputEnvVars, err := GetInputEnvVarsFromStrings(params.EnvVars)
if err != nil {
return errors.Wrapf(err, "error adding environment variables to the container")
}
// generate and create ImageStream
is := imagev1.ImageStream{
ObjectMeta: commonObjectMeta,
}
_, err = c.imageClient.ImageStreams(c.Namespace).Create(&is)
if err != nil {
return errors.Wrapf(err, "unable to create ImageStream for %s", commonObjectMeta.Name)
}
// if gitURL is not set, error out
if params.SourcePath == "" {
return errors.New("unable to create buildSource with empty gitURL")
}
// Deploy BuildConfig to build the container with Git
buildConfig, err := c.CreateBuildConfig(commonObjectMeta, params.ImageName, params.SourcePath, params.SourceRef, inputEnvVars)
if err != nil {
return errors.Wrapf(err, "unable to deploy BuildConfig for %s", commonObjectMeta.Name)
}
// Generate and create the DeploymentConfig
dc := generateGitDeploymentConfig(commonObjectMeta, buildConfig.Spec.Output.To.Name, containerPorts, inputEnvVars, params.Resources)
err = addOrRemoveVolumeAndVolumeMount(c, &dc, params.StorageToBeMounted, nil)
if err != nil {
return errors.Wrapf(err, "failed to mount and unmount pvc to dc")
}
createdDC, err := c.appsClient.DeploymentConfigs(c.Namespace).Create(&dc)
if err != nil {
return errors.Wrapf(err, "unable to create DeploymentConfig for %s", commonObjectMeta.Name)
}
ownerReference := generateOwnerReference(createdDC)
// update the owner references for the new storage
for _, storage := range params.StorageToBeMounted {
err := updateStorageOwnerReference(c, storage, ownerReference)
if err != nil {
return errors.Wrapf(err, "unable to update owner reference of storage")
}
}
// Create a service
svc, err := c.CreateService(commonObjectMeta, dc.Spec.Template.Spec.Containers[0].Ports, ownerReference)
if err != nil {
return errors.Wrapf(err, "unable to create Service for %s", commonObjectMeta.Name)
}
// Create secret(s)
err = c.createSecrets(params.Name, commonObjectMeta, svc, ownerReference)
return err
}
// Create a secret for each port, containing the host and port of the component
// This is done so other components can later inject the secret into the environment
// and have the "coordinates" to communicate with this component
func (c *Client) createSecrets(componentName string, commonObjectMeta metav1.ObjectMeta, svc *corev1.Service, ownerReference metav1.OwnerReference) error {
originalName := commonObjectMeta.Name
for _, svcPort := range svc.Spec.Ports {
portAsString := fmt.Sprintf("%v", svcPort.Port)
// we need to create multiple secrets, so each one has to contain the port in it's name
// so we change the name of each secret by adding the port number
commonObjectMeta.Name = fmt.Sprintf("%v-%v", originalName, portAsString)
// we also add the port as an annotation to the secret
// this comes in handy when we need to "query" for the appropriate secret
// of a component based on the port
commonObjectMeta.Annotations[ComponentPortAnnotationName] = portAsString
err := c.CreateSecret(
commonObjectMeta,
map[string]string{
secretKeyName(componentName, "host"): svc.Name,
secretKeyName(componentName, "port"): portAsString,
},
ownerReference)
if err != nil {
return errors.Wrapf(err, "unable to create Secret for %s", commonObjectMeta.Name)
}
}
// restore the original values of the fields we changed
commonObjectMeta.Name = originalName
delete(commonObjectMeta.Annotations, ComponentPortAnnotationName)
return nil
}
func secretKeyName(componentName, baseKeyName string) string {
return fmt.Sprintf("COMPONENT_%v_%v", strings.Replace(strings.ToUpper(componentName), "-", "_", -1), strings.ToUpper(baseKeyName))
}
// getS2ILabelValue returns the requested S2I label value from the passed set of labels attached to builder image
// and the hard coded possible list(the labels are not uniform across different builder images) of expected labels
func getS2ILabelValue(labels map[string]string, expectedLabelsSet []string) string {
for _, label := range expectedLabelsSet {
if retVal, ok := labels[label]; ok {
return retVal
}
}
return ""
}
// GetS2IMetaInfoFromBuilderImg returns script path protocol, S2I scripts path, S2I source or binary expected path, S2I deployment dir and errors(if any) from the passed builder image
func GetS2IMetaInfoFromBuilderImg(builderImage *imagev1.ImageStreamImage) (S2IPaths, error) {
// Define structs for internal un-marshalling of imagestreamimage to extract label from it
type ContainerConfig struct {
Labels map[string]string `json:"Labels"`
WorkingDir string `json:"WorkingDir"`
}
type DockerImageMetaDataRaw struct {
ContainerConfig ContainerConfig `json:"ContainerConfig"`
}
var dimdr DockerImageMetaDataRaw
// The label $S2IScriptsURLLabel needs to be extracted from builderImage#Image#DockerImageMetadata#Raw which is byte array
dimdrByteArr := (*builderImage).Image.DockerImageMetadata.Raw
// Unmarshal the byte array into the struct for ease of access of required fields
err := json.Unmarshal(dimdrByteArr, &dimdr)
if err != nil {
return S2IPaths{}, errors.Wrap(err, "unable to bootstrap supervisord")
}
// If by any chance, labels attribute is nil(although ideally not the case for builder images), return
if dimdr.ContainerConfig.Labels == nil {
glog.V(4).Infof("No Labels found in %+v in builder image %+v", dimdr, builderImage)
return S2IPaths{}, nil
}
// Extract the label containing S2I scripts URL
s2iScriptsURL := dimdr.ContainerConfig.Labels[S2IScriptsURLLabel]
s2iSrcOrBinPath := dimdr.ContainerConfig.Labels[S2ISrcOrBinLabel]
s2iBuilderImgName := dimdr.ContainerConfig.Labels[S2IBuilderImageName]
if s2iSrcOrBinPath == "" {
// In cases like nodejs builder image, where there is no concept of binary and sources are directly run, use destination as source
// s2iSrcOrBinPath = getS2ILabelValue(dimdr.ContainerConfig.Labels, S2IDeploymentsDir)
s2iSrcOrBinPath = DefaultS2ISrcOrBinPath
}
s2iDestinationDir := getS2ILabelValue(dimdr.ContainerConfig.Labels, S2IDeploymentsDir)
// The URL is a combination of protocol and the path to script details of which can be found @
// https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#s2i-scripts
// Extract them out into protocol and path separately to minimise the task in
// https://github.com/openshift/odo-init-image/blob/master/assemble-and-restart when custom handling
// for each of the protocols is added
s2iScriptsProtocol := ""
s2iScriptsPath := ""
switch {
case strings.HasPrefix(s2iScriptsURL, "image://"):
s2iScriptsProtocol = "image://"
s2iScriptsPath = strings.TrimPrefix(s2iScriptsURL, "image://")