File: gceutil.go

package info (click to toggle)
golang-go4 0.0~git20201209.d4a0794-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 816 kB
  • sloc: asm: 20; makefile: 4
file content (132 lines) | stat: -rw-r--r-- 3,803 bytes parent folder | download | duplicates (2)
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
/*
Copyright 2015 The Perkeep Authors

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 gceutil provides utility functions to help with instances on
// Google Compute Engine.
package gceutil // import "go4.org/cloud/google/gceutil"

import (
	"encoding/json"
	"errors"
	"net/http"
	"strings"
	"time"

	"google.golang.org/api/compute/v1"
)

// CoreOSImageURL returns the URL of the latest stable CoreOS image for running
// on Google Compute Engine.
func CoreOSImageURL(cl *http.Client) (string, error) {
	return osImageURL(cl, false)
}

// COSImageURL returns the URL of the latest stable Container-Optimized OS image
// for running on Google Compute Engine.
func COSImageURL(cl *http.Client) (string, error) {
	return osImageURL(cl, true)
}

func osImageURL(cl *http.Client, cos bool) (string, error) {
	project := "coreos-cloud"
	if cos {
		project = "cos-cloud"
	}
	resp, err := cl.Get("https://www.googleapis.com/compute/v1/projects/" + project + "/global/images")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	type osImage struct {
		SelfLink          string
		CreationTimestamp time.Time
		Name              string
	}

	type osImageList struct {
		Items []osImage
	}

	imageList := &osImageList{}
	if err := json.NewDecoder(resp.Body).Decode(imageList); err != nil {
		return "", err
	}
	if imageList == nil || len(imageList.Items) == 0 {
		return "", errors.New("no images list in response")
	}

	imageURL := ""
	var max time.Time // latest stable image creation time
	imgPrefix := "coreos-stable"
	if cos {
		imgPrefix = "cos-stable"
	}
	for _, v := range imageList.Items {
		if !strings.HasPrefix(v.Name, imgPrefix) {
			continue
		}
		if v.CreationTimestamp.After(max) {
			max = v.CreationTimestamp
			imageURL = v.SelfLink
		}
	}
	if imageURL == "" {
		if cos {
			return "", errors.New("no stable Container-Optimized OS image found")
		}
		return "", errors.New("no stable coreOS image found")
	}
	return imageURL, nil
}

// InstanceGroupAndManager contains both an InstanceGroup and
// its InstanceGroupManager, if any.
type InstanceGroupAndManager struct {
	Group *compute.InstanceGroup

	// Manager is the manager of the Group. It may be nil.
	Manager *compute.InstanceGroupManager
}

// InstanceGroups returns all the instance groups in a project's zone, along
// with their associated InstanceGroupManagers.
// The returned map is keyed by the instance group identifier URL.
func InstanceGroups(svc *compute.Service, proj, zone string) (map[string]InstanceGroupAndManager, error) {
	managerList, err := svc.InstanceGroupManagers.List(proj, zone).Do()
	if err != nil {
		return nil, err
	}
	if managerList.NextPageToken != "" {
		return nil, errors.New("too many managers; pagination not supported")
	}
	managedBy := make(map[string]*compute.InstanceGroupManager) // instance group URL -> its manager
	for _, it := range managerList.Items {
		managedBy[it.InstanceGroup] = it
	}
	groupList, err := svc.InstanceGroups.List(proj, zone).Do()
	if err != nil {
		return nil, err
	}
	if groupList.NextPageToken != "" {
		return nil, errors.New("too many instance groups; pagination not supported")
	}
	ret := make(map[string]InstanceGroupAndManager)
	for _, it := range groupList.Items {
		ret[it.SelfLink] = InstanceGroupAndManager{it, managedBy[it.SelfLink]}
	}
	return ret, nil
}