/
metadata_processor.go
142 lines (121 loc) · 4.8 KB
/
metadata_processor.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
// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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
//
// http://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 importer
import (
"fmt"
"strings"
daisy "github.com/GoogleCloudPlatform/compute-daisy"
daisyCompute "github.com/GoogleCloudPlatform/compute-daisy/compute"
"google.golang.org/api/compute/v1"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/daisyutils"
)
// metadataProcessor ensures metadata is present on a GCP disk. If
// metadata is missing, the disk will be recreated.
type metadataProcessor struct {
project, zone string
computeDiskClient daisyCompute.Client
requiredLicenses []string
requiredFeatures []*compute.GuestOsFeature
}
func newMetadataProcessor(
project string,
zone string,
computeDiskClient daisyCompute.Client) *metadataProcessor {
return &metadataProcessor{project: project, zone: zone, computeDiskClient: computeDiskClient}
}
func (p *metadataProcessor) process(pd persistentDisk) (persistentDisk, error) {
// Fast path 1: No modification requested.
if len(p.requiredFeatures) == 0 && len(p.requiredLicenses) == 0 {
return pd, nil
}
// Fast path 2: The current disk already has the requested modifications present.
newDisk, changesRequired, err := p.stageRequestForNewDisk(pd)
if !changesRequired || err != nil {
return pd, err
}
// Slow path: Clone then delete the existing disk and return a reference
// to the new disk.
err = p.computeDiskClient.CreateDisk(p.project, p.zone, newDisk)
if err != nil {
return pd, daisy.Errf("Failed to clone disk: %v", err)
}
// Delete the old disk after the new disk is created.
deleteDisk(p.computeDiskClient, p.project, p.zone, pd)
// Update the new disk URI
pd.uri = fmt.Sprintf("zones/%v/disks/%v", p.zone, newDisk.Name)
return pd, nil
}
// stageRequestForNewDisk fetches the disk associated with `pd`, and creates a compute.Disk
// struct that contains requested modifications.
//
// When false, `cloneRequired` signifies that the current disk satisfies all requested changes.
func (p *metadataProcessor) stageRequestForNewDisk(pd persistentDisk) (newDisk *compute.Disk, cloneRequired bool, err error) {
diskName := daisyutils.GetResourceID(pd.uri)
currentDisk, err := p.computeDiskClient.GetDisk(p.project, p.zone, diskName)
if err != nil {
return nil, false, daisy.Errf("Failed to get disk: %v", err)
}
newDiskName := daisyutils.GenerateValidDisksImagesName(fmt.Sprintf("%v-1", diskName))
newDisk = &compute.Disk{
Name: newDiskName,
SourceDisk: pd.uri,
}
if len(currentDisk.GuestOsFeatures) > 0 {
newDisk.GuestOsFeatures = make([]*compute.GuestOsFeature, len(currentDisk.GuestOsFeatures))
copy(newDisk.GuestOsFeatures, currentDisk.GuestOsFeatures)
}
if len(currentDisk.Licenses) > 0 {
newDisk.Licenses = make([]string, len(currentDisk.Licenses))
copy(newDisk.Licenses, currentDisk.Licenses)
}
cloneRequired = false
for _, feature := range p.requiredFeatures {
if !hasGuestOSFeature(currentDisk, feature) {
newDisk.GuestOsFeatures = append(newDisk.GuestOsFeatures, feature)
cloneRequired = true
}
}
for _, license := range p.requiredLicenses {
if !hasLicense(currentDisk, license) {
newDisk.Licenses = append(newDisk.Licenses, license)
cloneRequired = true
}
}
return newDisk, cloneRequired, nil
}
func hasLicense(existingDisk *compute.Disk, requestedLicense string) bool {
for _, foundLicense := range existingDisk.Licenses {
// Licenses are expressed as [GCP resources](https://cloud.google.com/apis/design/resource_names),
// which can either be a full URL, or a path. Both of these are valid:
// - https://www.googleapis.com/compute/v1/projects/windows-cloud/global/licenses/windows-10-enterprise-byol
// - projects/windows-cloud/global/licenses/windows-10-enterprise-byol
// The double suffix match supports all combinations of full URI and path.
if strings.HasSuffix(foundLicense, requestedLicense) || strings.HasSuffix(requestedLicense, foundLicense) {
return true
}
}
return false
}
func hasGuestOSFeature(existingDisk *compute.Disk, feature *compute.GuestOsFeature) bool {
for _, f := range existingDisk.GuestOsFeatures {
if f.Type == feature.Type {
return true
}
}
return false
}
func (p *metadataProcessor) cancel(reason string) bool {
// Cancel is not performed since there is only one critical API call - CreateDisk
return false
}