-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
102 lines (86 loc) · 3.3 KB
/
types.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
// Copyright 2017 The Kubernetes Authors.
//
// 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 api
import (
"encoding/json"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
// SettingsConfigMapName contains a name of config map, that stores settings.
SettingsConfigMapName = "kubernetes-dashboard-settings"
// SettingsConfigMapNamespace contains a namespace of config map, that stores settings.
SettingsConfigMapNamespace = "kube-system"
// ConfigMapKindName is a name of config map kind.
ConfigMapKindName = "ConfigMap"
// ConfigMapAPIVersion is a API version of config map.
ConfigMapAPIVersion = "v1"
// GlobalSettingsKey is a settings map key which maps to current global settings.
GlobalSettingsKey = "_global"
// ConcurrentSettingsChangeError occurs during settings save if settings were modified concurrently.
// Keep it in sync with CONCURRENT_CHANGE_ERROR constant from the frontend.
ConcurrentSettingsChangeError = "settings changed since last reload"
)
// SettingsManager is used for user settings management.
type SettingsManager interface {
// GetGlobalSettings gets current global settings from config map.
GetGlobalSettings(client kubernetes.Interface) (s *Settings)
// SaveGlobalSettings saves provided global settings in config map.
SaveGlobalSettings(client kubernetes.Interface, s *Settings)
}
// Settings is a single instance of settings without context.
type Settings struct {
ClusterName string `json:"clusterName"`
ItemsPerPage int `json:"itemsPerPage"`
AutoRefreshTimeInterval int `json:"autoRefreshTimeInterval"`
}
// Marshal settings into JSON object.
func (s Settings) Marshal() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
// Unmarshal settings from JSON string into object.
func Unmarshal(data string) (*Settings, error) {
s := new(Settings)
err := json.Unmarshal([]byte(data), s)
return s, err
}
// defaultSettings contains default values for every setting.
var defaultSettings = Settings{
ClusterName: "",
ItemsPerPage: 10,
AutoRefreshTimeInterval: 5,
}
// GetDefaultSettings returns settings structure, that should be used if there are no
// global or local settings overriding them. It should not change during runtime.
func GetDefaultSettings() Settings {
return defaultSettings
}
// GetDefaultSettingsConfigMap returns config map with default settings.
func GetDefaultSettingsConfigMap() *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: SettingsConfigMapName,
Namespace: SettingsConfigMapNamespace,
},
TypeMeta: metav1.TypeMeta{
Kind: ConfigMapKindName,
APIVersion: ConfigMapAPIVersion,
},
Data: map[string]string{
GlobalSettingsKey: GetDefaultSettings().Marshal(),
},
}
}