-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
45 lines (37 loc) · 1.23 KB
/
Copy pathclient.go
File metadata and controls
45 lines (37 loc) · 1.23 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
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"go.datum.net/datumctl/plugin"
inventoryv1alpha2 "go.miloapis.com/inventory/api/v1alpha2"
)
// newClient builds a controller-runtime client against the Datum Cloud platform
// root, where inventory objects live. It reads the API host from the context
// datumctl injects and fetches a fresh token via the credentials helper.
func newClient() (client.Client, error) {
ctx := plugin.Context()
if ctx.APIHost == "" {
return nil, fmt.Errorf("DATUM_API_HOST is not set; run this via 'datumctl inventory ...' (not the bare binary)")
}
token, err := plugin.Token()
if err != nil {
return nil, fmt.Errorf("get credentials: %w", err)
}
host := ctx.APIHost
if !strings.Contains(host, "://") {
host = "https://" + host
}
scheme := runtime.NewScheme()
if err := inventoryv1alpha2.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("build scheme: %w", err)
}
c, err := client.New(&rest.Config{Host: host, BearerToken: token}, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("build API client: %w", err)
}
return c, nil
}