-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathrest.go
More file actions
181 lines (155 loc) · 6.55 KB
/
Copy pathrest.go
File metadata and controls
181 lines (155 loc) · 6.55 KB
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
package agent
import (
"context"
"net"
"net/url"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/openshift/assisted-service/api/v1beta1"
"github.com/openshift/assisted-service/client"
"github.com/openshift/assisted-service/client/events"
"github.com/openshift/assisted-service/client/installer"
"github.com/openshift/assisted-service/models"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/agent/agentconfig"
"github.com/openshift/installer/pkg/asset/agent/gencrypto"
"github.com/openshift/installer/pkg/asset/agent/image"
"github.com/openshift/installer/pkg/asset/agent/manifests"
"github.com/openshift/installer/pkg/types/agent"
)
// NodeZeroRestClient is a struct to interact with the Agent Rest API that is on node zero.
type NodeZeroRestClient struct {
Client *client.AssistedInstall
ctx context.Context
config client.Config
NodeZeroIP string
}
// NewNodeZeroRestClient Initialize a new rest client to interact with the Agent Rest API on node zero.
func NewNodeZeroRestClient(ctx context.Context, rendezvousIP, watcherAuthToken string) *NodeZeroRestClient {
restClient := &NodeZeroRestClient{}
config := client.Config{}
config.URL = &url.URL{
Scheme: "http",
Host: net.JoinHostPort(rendezvousIP, "8090"),
Path: client.DefaultBasePath,
}
config.AuthInfo = gencrypto.WatcherAuthHeaderWriter(watcherAuthToken)
client := client.New(config)
restClient.Client = client
restClient.ctx = ctx
restClient.config = config
restClient.NodeZeroIP = rendezvousIP
return restClient
}
// FindRendezvousIPFromAssetStore returns the rendezvous IP of the agent cluster.
func FindRendezvousIPFromAssetStore(assetStore asset.Store) (string, error) {
agentConfigAsset := &agentconfig.AgentConfig{}
agentManifestsAsset := &manifests.AgentManifests{}
agentHostsAsset := &agentconfig.AgentHosts{}
agentConfig, agentConfigError := assetStore.Load(agentConfigAsset)
agentManifests, manifestError := assetStore.Load(agentManifestsAsset)
agentHosts, agentHostsError := assetStore.Load(agentHostsAsset)
if agentConfigError != nil {
logrus.Debug(errors.Wrapf(agentConfigError, "failed to load %s", agentConfigAsset.Name()))
}
if manifestError != nil {
logrus.Debug(errors.Wrapf(manifestError, "failed to load %s", agentManifestsAsset.Name()))
}
if agentHostsError != nil {
logrus.Debug(errors.Wrapf(agentHostsError, "failed to load %s", agentHostsAsset.Name()))
}
if agentConfigError != nil || manifestError != nil || agentHostsError != nil {
return "", errors.New("failed to load AgentConfig, NMStateConfig, or AgentHosts")
}
var rendezvousIP string
var rendezvousIPError error
var emptyNMStateConfigs []*v1beta1.NMStateConfig
if agentConfig != nil && agentManifests != nil {
rendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, agentHosts.(*agentconfig.AgentHosts).Hosts, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
} else if agentConfig == nil && agentManifests != nil {
rendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(&agent.Config{}, agentHosts.(*agentconfig.AgentHosts).Hosts, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
} else if agentConfig != nil && agentManifests == nil {
rendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, agentHosts.(*agentconfig.AgentHosts).Hosts, emptyNMStateConfigs)
} else {
return "", errors.New("both AgentConfig and NMStateConfig are empty")
}
if rendezvousIPError != nil {
return "", rendezvousIPError
}
return rendezvousIP, nil
}
// FindAuthTokenFromAssetStore returns the auth token from asset store.
func FindAuthTokenFromAssetStore(assetStore asset.Store) (string, error) {
authConfigAsset := &gencrypto.AuthConfig{}
authConfig, authConfigError := assetStore.Load(authConfigAsset)
if authConfigError != nil {
logrus.Debug(errors.Wrapf(authConfigError, "failed to load %s", authConfigAsset.Name()))
return "", errors.New("failed to load AuthConfig")
}
var authToken string
if authConfig != nil {
authToken = authConfig.(*gencrypto.AuthConfig).WatcherAuthToken
}
return authToken, nil
}
// IsRestAPILive Determine if the Agent Rest API on node zero has initialized
func (rest *NodeZeroRestClient) IsRestAPILive() bool {
// GET /v2/infraenvs
listInfraEnvsParams := installer.NewListInfraEnvsParams()
_, err := rest.Client.Installer.ListInfraEnvs(rest.ctx, listInfraEnvsParams)
return err == nil
}
// GetRestAPIServiceBaseURL Return the url of the Agent Rest API on node zero
func (rest *NodeZeroRestClient) GetRestAPIServiceBaseURL() *url.URL {
return rest.config.URL
}
// GetInfraEnvEvents Return the event list for the provided infraEnvID from the Agent Rest API
func (rest *NodeZeroRestClient) GetInfraEnvEvents(infraEnvID *strfmt.UUID) (models.EventList, error) {
listEventsParams := &events.V2ListEventsParams{InfraEnvID: infraEnvID}
clusterEventsResult, err := rest.Client.Events.V2ListEvents(rest.ctx, listEventsParams)
if err != nil {
return nil, err
}
return clusterEventsResult.Payload, nil
}
// getClusterID Return the cluster ID assigned by the Agent Rest API
func (rest *NodeZeroRestClient) getClusterID() (*strfmt.UUID, error) {
// GET /v2/clusters and return first result
listClusterParams := installer.NewV2ListClustersParams()
clusterResult, err := rest.Client.Installer.V2ListClusters(rest.ctx, listClusterParams)
if err != nil {
return nil, err
}
clusterList := clusterResult.Payload
if len(clusterList) == 1 {
clusterID := clusterList[0].ID
return clusterID, nil
} else if len(clusterList) == 0 {
logrus.Debug("cluster is not registered in rest API")
return nil, nil
} else {
logrus.Infof("found too many clusters. number of clusters found: %d", len(clusterList))
return nil, nil
}
}
// getClusterID Return the infraEnv ID associated with the cluster in the Agent Rest API
func (rest *NodeZeroRestClient) getClusterInfraEnvID() (*strfmt.UUID, error) {
// GET /v2/infraenvs and return first result
listInfraEnvParams := installer.NewListInfraEnvsParams()
infraEnvResult, err := rest.Client.Installer.ListInfraEnvs(rest.ctx, listInfraEnvParams)
if err != nil {
return nil, err
}
infraEnvList := infraEnvResult.Payload
if len(infraEnvList) == 1 {
clusterInfraEnvID := infraEnvList[0].ID
return clusterInfraEnvID, nil
} else if len(infraEnvList) == 0 {
logrus.Debug("infraenv is not registered in rest API")
return nil, nil
} else {
logrus.Infof("found too many infraenvs. number of infraenvs found: %d", len(infraEnvList))
return nil, nil
}
}