|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package deployer |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "k8s.io/klog/v2" |
| 25 | + "sigs.k8s.io/kubetest2/pkg/boskos" |
| 26 | +) |
| 27 | + |
| 28 | +func (d *deployer) acquireBoskosAWSAccount() error { |
| 29 | + klog.V(1).Info("Acquiring AWS account from Boskos") |
| 30 | + |
| 31 | + boskosClient, err := boskos.NewClient(d.BoskosLocation) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to make boskos client: %s", err) |
| 34 | + } |
| 35 | + d.boskos = boskosClient |
| 36 | + |
| 37 | + resource, err := boskos.Acquire( |
| 38 | + d.boskos, |
| 39 | + d.BoskosAWSResourceType, |
| 40 | + d.BoskosAcquireTimeout, |
| 41 | + d.BoskosHeartbeatInterval, |
| 42 | + d.boskosHeartbeatClose, |
| 43 | + ) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("init failed to get aws account from boskos: %s", err) |
| 46 | + } |
| 47 | + d.boskosAWSAccount = resource.Name |
| 48 | + klog.V(1).Infof("Got aws account %s from boskos", d.boskosAWSAccount) |
| 49 | + |
| 50 | + var data struct { |
| 51 | + AccessKeyID string `json:"access_key_id"` |
| 52 | + SecretAccessKey string `json:"secret_access_key"` |
| 53 | + SessionToken string `json:"session_token"` |
| 54 | + } |
| 55 | + if err := json.Unmarshal([]byte(resource.UserData), &data); err != nil { |
| 56 | + return fmt.Errorf("failed to unmarshal boskos user data: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + os.Setenv("AWS_ACCESS_KEY_ID", data.AccessKeyID) |
| 60 | + os.Setenv("AWS_SECRET_ACCESS_KEY", data.SecretAccessKey) |
| 61 | + os.Setenv("AWS_SESSION_TOKEN", data.SessionToken) |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (d *deployer) acquireBoskosGCPProject() error { |
| 67 | + klog.V(1).Info("No GCP project provided, acquiring from Boskos") |
| 68 | + |
| 69 | + boskosClient, err := boskos.NewClient(d.BoskosLocation) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to make boskos client: %s", err) |
| 72 | + } |
| 73 | + d.boskos = boskosClient |
| 74 | + |
| 75 | + resource, err := boskos.Acquire( |
| 76 | + d.boskos, |
| 77 | + d.BoskosGCPResourceType, |
| 78 | + d.BoskosAcquireTimeout, |
| 79 | + d.BoskosHeartbeatInterval, |
| 80 | + d.boskosHeartbeatClose, |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("init failed to get project from boskos: %s", err) |
| 84 | + } |
| 85 | + d.GCPProject = resource.Name |
| 86 | + klog.V(1).Infof("Got project %s from boskos", d.GCPProject) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func (d *deployer) releaseBoskosResources() error { |
| 91 | + if d.boskos == nil { |
| 92 | + return nil |
| 93 | + } |
| 94 | + klog.V(2).Info("releasing boskos project") |
| 95 | + resources := []string{} |
| 96 | + if d.GCPProject != "" { |
| 97 | + resources = append(resources, d.GCPProject) |
| 98 | + } |
| 99 | + if d.boskosAWSAccount != "" { |
| 100 | + resources = append(resources, d.boskosAWSAccount) |
| 101 | + } |
| 102 | + err := boskos.Release( |
| 103 | + d.boskos, |
| 104 | + resources, |
| 105 | + d.boskosHeartbeatClose, |
| 106 | + ) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("down failed to release boskos project: %s", err) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
0 commit comments