forked from kubernetes-sigs/apiserver-runtime
/
fortune_storage.go
87 lines (78 loc) · 2.35 KB
/
fortune_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
package v1alpha1
import (
"context"
"fmt"
"os/exec"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/zachaller/apiserver-runtime/pkg/builder/resource/resourcerest"
)
// Rest functions
var _ resourcerest.Getter = &Fortune{}
var _ resourcerest.Lister = &Fortune{}
var _ resourcerest.TableConvertor = &Fortune{}
// ConvertToTable handles table printing from kubectl get
func (f *Fortune) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
switch o := object.(type) {
case *Fortune:
return o.convertToTable(ctx, tableOptions)
case *FortuneList:
return o.convertToTable(ctx, tableOptions)
}
return nil, fmt.Errorf("unknown type Fortune %T", f)
}
// List implements rest.Lister
func (f *Fortune) List(ctx context.Context, o *internalversion.ListOptions) (runtime.Object, error) {
parts := strings.SplitN(o.LabelSelector.String(), "=", 2)
if len(parts) == 1 {
fl := &FortuneList{}
// return 5 random fortunes
for i := 0; i < 5; i++ {
obj, err := f.Get(ctx, "", &metav1.GetOptions{})
if err != nil {
return nil, err
}
fl.Items = append(fl.Items, *obj.(*Fortune))
}
return fl, nil
}
fl := &FortuneList{}
var out []byte
/* #nosec */
out, _ = exec.Command("/usr/games/fortune", "-s", "-m", parts[1]).Output()
values := strings.Split(string(out), "\n%\n")
for i, fo := range values {
if i > 5 {
break
}
if strings.TrimSpace(fo) == "" {
continue
}
fl.Items = append(fl.Items, Fortune{Value: strings.TrimSpace(fo)})
}
return fl, nil
}
// Get implements rest.Getter
func (f *Fortune) Get(_ context.Context, name string, _ *metav1.GetOptions) (runtime.Object, error) {
obj := &Fortune{}
var out []byte
// fortune exits non-zero on success
if name == "" {
out, _ = exec.Command("/usr/games/fortune", "-s").Output()
} else {
/* #nosec */
out, _ = exec.Command("/usr/games/fortune", "-s", "-m", name).Output()
fortunes := strings.Split(string(out), "\n%\n")
if len(fortunes) > 0 {
out = []byte(fortunes[0])
}
}
if len(strings.TrimSpace(string(out))) == 0 {
return nil, errors.NewNotFound(Fortune{}.GetGroupVersionResource().GroupResource(), name)
}
obj.Value = strings.TrimSpace(string(out))
return obj, nil
}