-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.go
137 lines (114 loc) · 3.42 KB
/
storage.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
/*
Copyright AppsCode Inc. and Contributors.
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 reports
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
costapi "kubeops.dev/ui-server/apis/cost/v1alpha1"
gs "github.com/gorilla/schema"
"github.com/pkg/errors"
"gomodules.xyz/sync"
core "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
once sync.Once
ocURL *url.URL
)
type Storage struct {
kc client.Client
}
var (
_ rest.GroupVersionKindProvider = &Storage{}
_ rest.Scoper = &Storage{}
_ rest.Storage = &Storage{}
_ rest.Creater = &Storage{}
_ rest.SingularNameProvider = &Storage{}
)
func NewStorage(kc client.Client) *Storage {
return &Storage{
kc: kc,
}
}
func (r *Storage) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind {
return costapi.SchemeGroupVersion.WithKind(costapi.ResourceKindCostReport)
}
func (r *Storage) NamespaceScoped() bool {
return false
}
func (r *Storage) GetSingularName() string {
return strings.ToLower(costapi.ResourceKindCostReport)
}
func (r *Storage) New() runtime.Object {
return &costapi.CostReport{}
}
func (r *Storage) Destroy() {}
func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.ValidateObjectFunc, _ *metav1.CreateOptions) (runtime.Object, error) {
in := obj.(*costapi.CostReport)
once.Do(func() error {
var svcs core.ServiceList
err := r.kc.List(ctx, &svcs, client.MatchingLabels{
"app.kubernetes.io/name": "opencost",
})
if err != nil {
return err
} else if len(svcs.Items) == 0 {
return errors.New("opencost service not found")
}
ocSvc := svcs.Items[0]
var ocApiPort int32
for _, p := range ocSvc.Spec.Ports {
if p.Name == "http" {
ocApiPort = p.Port
break
}
}
if ocApiPort <= 0 {
return errors.New("missing http port in opencost service spec")
}
ocURL, err = url.Parse(fmt.Sprintf("http://%s.%s.svc:%d/model/allocation/compute", ocSvc.Name, ocSvc.Namespace, ocApiPort))
return err
})
encoder := gs.NewEncoder()
form := url.Values{}
err := encoder.Encode(in.Request, form)
if err != nil {
return nil, errors.Wrap(err, "failed to encode cost report request")
}
u := *ocURL
u.RawQuery = form.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create opencost service request")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to call opencost service")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read opencost api response")
}
in.Response = &apiextensionsv1.JSON{Raw: body}
return in, nil
}