Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 382d614

Browse files
thebsdboxDavid Chung
authored andcommitted
Initial OneView plugin (#731)
Signed-off-by: Dan Finneran <[email protected]>
1 parent ccbd398 commit 382d614

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+6808
-0
lines changed

cmd/infrakit/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
_ "github.com/docker/infrakit/pkg/run/v0/kubernetes"
5454
_ "github.com/docker/infrakit/pkg/run/v0/maas"
5555
_ "github.com/docker/infrakit/pkg/run/v0/manager"
56+
_ "github.com/docker/infrakit/pkg/run/v0/oneview"
5657
_ "github.com/docker/infrakit/pkg/run/v0/oracle"
5758
_ "github.com/docker/infrakit/pkg/run/v0/packet"
5859
_ "github.com/docker/infrakit/pkg/run/v0/rackhd"

pkg/provider/oneview/plugin.go

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
package oneview
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"os"
7+
"strings"
8+
"sync"
9+
"time"
10+
11+
logutil "github.com/docker/infrakit/pkg/log"
12+
"github.com/docker/infrakit/pkg/spi"
13+
"github.com/docker/infrakit/pkg/spi/instance"
14+
"github.com/docker/infrakit/pkg/types"
15+
16+
"github.com/HewlettPackard/oneview-golang/ov"
17+
)
18+
19+
var log = logutil.New("module", "cli/x")
20+
21+
// Options capture the config parameters required to create the plugin
22+
type Options struct {
23+
OVUrl string
24+
OVUser string
25+
OVPass string
26+
OVCookie string
27+
OVApi int
28+
}
29+
30+
//miniFSM for managing the provisioning -> provisioned state
31+
type provisioningFSM struct {
32+
countdown int64 // ideally will be a counter of minutes / seconds
33+
tags map[string]string // tags that will be passed back per a describe function
34+
instanceName string // name that we will use as a lookup to the actual backend that is privisioning
35+
}
36+
37+
// Spec is just whatever that can be unmarshalled into a generic JSON map
38+
type Spec map[string]interface{}
39+
40+
// This contains the the details for the oneview instance
41+
type plugin struct {
42+
fsm []provisioningFSM
43+
client ov.OVClient
44+
}
45+
46+
var mux sync.Mutex
47+
48+
func init() {
49+
rand.Seed(time.Now().UTC().UnixNano())
50+
}
51+
52+
// NewOneViewInstancePlugin will take the cmdline/env configuration
53+
func NewOneViewInstancePlugin(ovOptions Options) instance.Plugin {
54+
55+
// Define client from params
56+
var client ov.OVClient
57+
client.Endpoint = ovOptions.OVUrl
58+
client.User = ovOptions.OVUser
59+
client.Password = ovOptions.OVPass
60+
client.APIVersion = ovOptions.OVApi
61+
62+
// Attempt to log in to HPE OneView, if a cookie is passed then just re-auth, or login with credentials
63+
session, err := client.SessionLogin()
64+
65+
// More verbose erroring might be needed i.e. https not http (also a protocl prefix)
66+
// Exit with an error if we can't connect to HPE OneView
67+
if err != nil {
68+
log.Crit("Error Logging into HPE OneView")
69+
os.Exit(-1)
70+
}
71+
72+
log.Debug("Succesfully logged in with sessionID %s", session.ID)
73+
74+
return &plugin{
75+
client: client,
76+
}
77+
}
78+
79+
// Info returns a vendor specific name and version
80+
func (p *plugin) VendorInfo() *spi.VendorInfo {
81+
return &spi.VendorInfo{
82+
InterfaceSpec: spi.InterfaceSpec{
83+
Name: "infrakit-instance-oneview",
84+
Version: "0.6.0",
85+
},
86+
URL: "https://github.com/docker/infrakit",
87+
}
88+
}
89+
90+
// ExampleProperties returns the properties / config of this plugin
91+
func (p *plugin) ExampleProperties() *types.Any {
92+
any, err := types.AnyValue(Spec{
93+
"exampleString": "a_string",
94+
"exampleBool": true,
95+
"exampleInt": 1,
96+
})
97+
if err != nil {
98+
return nil
99+
}
100+
return any
101+
}
102+
103+
// Validate performs local validation on a provision request.
104+
func (p *plugin) Validate(req *types.Any) error {
105+
log.Debug("validate", req.String())
106+
107+
spec := Spec{}
108+
if err := req.Decode(&spec); err != nil {
109+
return err
110+
}
111+
112+
log.Debug("Validated:", spec)
113+
return nil
114+
}
115+
116+
// Provision creates a new instance based on the spec.
117+
func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
118+
119+
var properties map[string]interface{}
120+
121+
if spec.Properties != nil {
122+
if err := spec.Properties.Decode(&properties); err != nil {
123+
return nil, fmt.Errorf("Invalid instance properties: %s", err)
124+
}
125+
}
126+
127+
instanceName := instance.ID(fmt.Sprintf("InfraKit-%d", rand.Int63()))
128+
129+
// Task isn't backgrounded with a goroutine as that caused numerous issues with teh web requests
130+
var template string
131+
132+
if val, ok := properties["TemplateName"]; ok {
133+
// Assign the string value
134+
template = val.(string)
135+
} else {
136+
log.Error("InfraKit Tag TemplateName has been left blank")
137+
}
138+
139+
profileTemplate, err := p.client.GetProfileTemplateByName(template)
140+
if err != nil {
141+
log.Warn("Error returning list of profiles %v", err)
142+
}
143+
availHW, err := p.client.GetAvailableHardware(profileTemplate.ServerHardwareTypeURI, profileTemplate.EnclosureGroupURI)
144+
if err != nil {
145+
log.Warn("Error returning list of profiles %v", err)
146+
}
147+
148+
// Build a custom Description to allow InfraKit to identify new Instances
149+
profileTemplate.Description = spec.Tags["infrakit.group"] + "|" + spec.Tags["infrakit.config_sha"]
150+
151+
err = p.createProfileFromTemplate(string(instanceName), profileTemplate, availHW, spec)
152+
if err != nil {
153+
log.Error("%v", err)
154+
}
155+
156+
if spec.Tags != nil {
157+
log.Info("Adding %s to Group %v", string(instanceName), spec.Tags["infrakit.group"])
158+
}
159+
160+
var newInstance provisioningFSM
161+
newInstance.instanceName = string(instanceName)
162+
newInstance.countdown = 5 // FIXED 10 minute timeout (TODO)
163+
164+
// duplicate the tags for the instance
165+
newInstance.tags = make(map[string]string)
166+
for k, v := range spec.Tags {
167+
newInstance.tags[k] = v
168+
}
169+
newInstance.tags["infrakit.state"] = "Provisioning"
170+
p.fsm = append(p.fsm, newInstance)
171+
log.Debug("New Instance added to state, count: %d", len(p.fsm))
172+
173+
return &instanceName, nil
174+
}
175+
176+
// Label labels the instance
177+
func (p *plugin) Label(instance instance.ID, labels map[string]string) error {
178+
return fmt.Errorf("HPE OneView label updates are not implemented yet")
179+
}
180+
181+
// Destroy terminates an existing instance.
182+
func (p *plugin) Destroy(instance instance.ID, context instance.Context) error {
183+
log.Info("Currently running %s on instance: %v", context, instance)
184+
return p.client.DeleteProfile(string(instance))
185+
}
186+
187+
// DescribeInstances returns descriptions of all instances matching all of the provided tags.
188+
// TODO - need to define the fitlering of tags => AND or OR of matches?
189+
func (p *plugin) DescribeInstances(tags map[string]string, properties bool) ([]instance.Description, error) {
190+
log.Debug("describe-instances", tags)
191+
results := []instance.Description{}
192+
193+
// Static search path for Profiles that are pre-fixed with the InfraKit- tag
194+
instances, err := p.client.GetProfiles("name matches 'InfraKit-%'", "")
195+
if err != nil {
196+
log.Warn("Error returning list of profiles %v", err)
197+
}
198+
199+
log.Debug("Found %d Profiles", instances.Total)
200+
201+
// Duplicate original tags
202+
for _, profile := range instances.Members {
203+
instanceTags := make(map[string]string)
204+
for k, v := range tags {
205+
instanceTags[k] = v
206+
}
207+
// Split the description field (single line)
208+
tagSlice := strings.Split(profile.Description, "|")
209+
// If it exists, grab the group name
210+
if len(tagSlice) > 0 {
211+
instanceTags["infrakit.group"] = tagSlice[0]
212+
}
213+
// If it exists, grab the sha
214+
if len(tagSlice) > 1 {
215+
instanceTags["infrakit.config_sha"] = tagSlice[1]
216+
}
217+
218+
// We're only wanting to return instances from a specific group
219+
if val, ok := tags["infrakit.group"]; ok {
220+
// Check that we can get the group from the profile
221+
if len(tagSlice) > 0 {
222+
// Does this group match, if so add it to the results
223+
if val == tagSlice[0] {
224+
results = append(results, instance.Description{
225+
ID: instance.ID(profile.Name),
226+
LogicalID: nil,
227+
Tags: instanceTags,
228+
})
229+
}
230+
}
231+
} else {
232+
// Return all
233+
results = append(results, instance.Description{
234+
ID: instance.ID(profile.Name),
235+
LogicalID: nil,
236+
Tags: instanceTags,
237+
})
238+
}
239+
}
240+
241+
log.Debug("Modifying provisining state count: %d", len(p.fsm))
242+
243+
// DIFF what the endpoint is saying as reported versus what we have in the FSM
244+
var updatedFSM []provisioningFSM
245+
for _, unprovisionedInstance := range p.fsm {
246+
var provisioned bool
247+
248+
for _, provisionedInstance := range results {
249+
250+
if string(provisionedInstance.ID) == unprovisionedInstance.instanceName {
251+
provisioned = true
252+
// instance has been provisioned so break from loop
253+
break
254+
} else {
255+
provisioned = false
256+
}
257+
}
258+
if provisioned == false && unprovisionedInstance.countdown != 0 && unprovisionedInstance.tags["infrakit.group"] == tags["infrakit.group"] {
259+
unprovisionedInstance.countdown--
260+
updatedFSM = append(updatedFSM, unprovisionedInstance)
261+
}
262+
}
263+
264+
p.fsm = make([]provisioningFSM, len(updatedFSM))
265+
copy(p.fsm, updatedFSM)
266+
267+
log.Debug("Updated provisining state count: %d", len(p.fsm))
268+
for _, unprovisionedInstances := range p.fsm {
269+
results = append(results, instance.Description{
270+
ID: instance.ID(unprovisionedInstances.instanceName),
271+
LogicalID: nil,
272+
Tags: unprovisionedInstances.tags,
273+
})
274+
}
275+
return results, nil
276+
}
277+
278+
// create profile from template
279+
func (p *plugin) createProfileFromTemplate(name string, template ov.ServerProfile, blade ov.ServerHardware, spec instance.Spec) error {
280+
log.Debug("TEMPLATE : %+v\n", template)
281+
var (
282+
newTemplate ov.ServerProfile
283+
err error
284+
)
285+
286+
if p.client.IsProfileTemplates() {
287+
log.Debug("getting profile by URI %+v, v2", template.URI)
288+
newTemplate, err = p.client.GetProfileByURI(template.URI)
289+
if err != nil {
290+
return err
291+
}
292+
newTemplate.Type = "ServerProfileV5"
293+
newTemplate.ServerProfileTemplateURI = template.URI // create relationship
294+
log.Debug("new_template -> %+v", newTemplate)
295+
} else {
296+
log.Debug("get new_template from clone, v1")
297+
newTemplate = template.Clone()
298+
}
299+
newTemplate.ServerHardwareURI = blade.URI
300+
// HPE OneView doesn't carry any concept of tags, we place all details needed in the Description field
301+
newTemplate.Description = spec.Tags["infrakit.group"] + "|" + spec.Tags["infrakit.config_sha"]
302+
newTemplate.Name = name
303+
304+
t, err := p.client.SubmitNewProfile(newTemplate)
305+
if err != nil {
306+
return err
307+
}
308+
// TODO: This prints out a lot of verbose text using a different logger, so will need changing.
309+
err = t.Wait()
310+
if err != nil {
311+
return err
312+
}
313+
314+
return nil
315+
}

0 commit comments

Comments
 (0)