feat: platform config API (#2052) (#6303)

Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
hongming
2025-02-28 16:48:44 +08:00
committed by GitHub
parent 018f6045ee
commit c1259aff8b
8 changed files with 318 additions and 48 deletions

View File

@@ -6,15 +6,22 @@
package v1alpha2
import (
"encoding/json"
"fmt"
"strings"
"github.com/emicklei/go-restful/v3"
jsonpatch "github.com/evanphx/json-patch/v5"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
@@ -22,12 +29,21 @@ import (
"kubesphere.io/kubesphere/pkg/apiserver/options"
"kubesphere.io/kubesphere/pkg/apiserver/rest"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/server/errors"
)
const (
themeConfigurationName = "platform-configuration-theme"
themeConfigurationName = "platform-configuration-theme"
GenericPlatformConfigurationKind = "GenericPlatformConfiguration"
)
type GenericPlatformConfiguration struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Data runtime.RawExtension `json:"data,omitempty"`
}
func NewHandler(config *options.Options, client client.Client) rest.Handler {
return &handler{
config: config,
@@ -106,3 +122,200 @@ func (h *handler) getOAuthConfiguration(req *restful.Request, resp *restful.Resp
}
_ = resp.WriteEntity(configuration)
}
func (h *handler) getConfigz(_ *restful.Request, response *restful.Response) {
_ = response.WriteAsJson(h.config)
}
func (h *handler) getPlatformConfiguration(request *restful.Request, response *restful.Response) {
configName := request.PathParameter("config")
if len(validation.IsDNS1123Label(configName)) > 0 {
api.HandleNotFound(response, request, fmt.Errorf("platform config %s not found", configName))
return
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: constants.KubeSphereNamespace,
Name: fmt.Sprintf(constants.GenericPlatformConfigNameFmt, configName),
},
}
if err := h.client.Get(request.Request.Context(), client.ObjectKeyFromObject(secret), secret); err != nil {
if apierrors.IsNotFound(err) {
api.HandleNotFound(response, request, fmt.Errorf("platform config %s not found", configName))
return
}
api.HandleError(response, request, err)
return
}
config := &GenericPlatformConfiguration{}
config.ConvertFromSecret(secret)
_ = response.WriteEntity(config)
}
func (h *handler) createPlatformConfiguration(request *restful.Request, response *restful.Response) {
config := &GenericPlatformConfiguration{}
if err := request.ReadEntity(config); err != nil {
api.HandleBadRequest(response, request, err)
return
}
if config.Kind != GenericPlatformConfigurationKind {
api.HandleBadRequest(response, request, fmt.Errorf("invalid kind: %s", config.Kind))
return
}
if config.APIVersion != APIVersion {
api.HandleBadRequest(response, request, fmt.Errorf("invalid apiVersion: %s", config.APIVersion))
return
}
if len(validation.IsDNS1123Label(config.Name)) > 0 {
api.HandleBadRequest(response, request, fmt.Errorf("invalid config name: %s", config.Name))
return
}
secret := config.ConvertToSecret()
if err := h.client.Create(request.Request.Context(), secret); err != nil {
api.HandleError(response, request, err)
return
}
config.ConvertFromSecret(secret)
_ = response.WriteEntity(config)
}
func (h *handler) patchPlatformConfiguration(request *restful.Request, response *restful.Response) {
var raw map[string]json.RawMessage
if err := request.ReadEntity(&raw); err != nil {
api.HandleBadRequest(response, request, err)
return
}
configName := request.PathParameter("config")
if len(validation.IsDNS1123Label(configName)) > 0 {
api.HandleNotFound(response, request, fmt.Errorf("platform config %s not found", configName))
return
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: constants.KubeSphereNamespace,
Name: fmt.Sprintf(constants.GenericPlatformConfigNameFmt, configName),
},
}
if err := h.client.Get(request.Request.Context(), client.ObjectKeyFromObject(secret), secret); err != nil {
if apierrors.IsNotFound(err) {
api.HandleNotFound(response, request, fmt.Errorf("platform config %s not found", configName))
return
}
api.HandleError(response, request, err)
return
}
config := &GenericPlatformConfiguration{}
config.ConvertFromSecret(secret)
original, err := config.Data.MarshalJSON()
if err != nil {
api.HandleError(response, request, err)
return
}
patchData := raw["data"]
if len(patchData) > 0 {
var modifiedData []byte
modifiedData, err = jsonpatch.MergePatch(original, patchData)
if err != nil {
api.HandleBadRequest(response, request, err)
return
}
if err = json.Unmarshal(modifiedData, &config.Data); err != nil {
api.HandleBadRequest(response, request, err)
return
}
secret = config.ConvertToSecret()
if err := h.client.Update(request.Request.Context(), secret); err != nil {
api.HandleError(response, request, err)
return
}
}
config.ConvertFromSecret(secret)
_ = response.WriteEntity(config)
}
func (h *handler) updatePlatformConfiguration(request *restful.Request, response *restful.Response) {
config := &GenericPlatformConfiguration{}
if err := request.ReadEntity(config); err != nil {
api.HandleBadRequest(response, request, err)
return
}
secret := config.ConvertToSecret()
if err := h.client.Update(request.Request.Context(), secret); err != nil {
api.HandleError(response, request, err)
return
}
config.ConvertFromSecret(secret)
_ = response.WriteEntity(config)
}
func (h *handler) deletePlatformConfiguration(request *restful.Request, response *restful.Response) {
configName := request.PathParameter("config")
if len(validation.IsDNS1123Label(configName)) > 0 {
api.HandleNotFound(response, request, fmt.Errorf("platform config %s not found", configName))
return
}
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: constants.KubeSphereNamespace,
Name: fmt.Sprintf(constants.GenericPlatformConfigNameFmt, configName),
},
}
if err := h.client.Delete(request.Request.Context(), &secret); err != nil {
api.HandleError(response, request, err)
return
}
_ = response.WriteEntity(errors.None)
}
func (c *GenericPlatformConfiguration) ConvertToSecret() *corev1.Secret {
yamlData, err := yaml.Marshal(c.Data)
if err != nil {
klog.Warningf("Failed to marshal platform configuration: %v", err)
return nil
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: constants.KubeSphereNamespace,
Name: fmt.Sprintf(constants.GenericPlatformConfigNameFmt, c.Name),
},
Type: constants.SecretTypeGenericPlatformConfig,
Data: map[string][]byte{
constants.GenericPlatformConfigFileName: yamlData,
},
}
return secret
}
func (c *GenericPlatformConfiguration) ConvertFromSecret(secret *corev1.Secret) {
c.Name = strings.TrimPrefix(secret.Name, fmt.Sprintf(constants.GenericPlatformConfigNameFmt, ""))
c.CreationTimestamp = secret.CreationTimestamp
c.ResourceVersion = secret.ResourceVersion
c.UID = secret.UID
c.Kind = GenericPlatformConfigurationKind
c.APIVersion = APIVersion
_ = yaml.Unmarshal(secret.Data[constants.GenericPlatformConfigFileName], &c.Data)
}

View File

@@ -9,50 +9,85 @@ import (
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1alpha1 "kubesphere.io/api/cluster/v1alpha1"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
)
const (
GroupName = "config.kubesphere.io"
GroupName = "config.kubesphere.io"
Version = "v1alpha2"
APIVersion = GroupName + "/" + Version
)
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}
func (h *handler) AddToContainer(c *restful.Container) error {
webservice := runtime.NewWebService(GroupVersion)
webservice.Route(webservice.GET("/configs/oauth").
Doc("OAuth configurations").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagPlatformConfigurations}).
Notes("Information about the authorization server are published.").
Operation("oauth-config").
To(h.getOAuthConfiguration))
webservice.Route(webservice.GET("/configs/configz").
Deprecate().
Doc("Component configurations").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagPlatformConfigurations}).
Notes("Information about the components configuration").
Operation("component-config").
To(func(request *restful.Request, response *restful.Response) {
_ = response.WriteAsJson(h.config)
}))
Doc("Retrieve multicluster configuration").
Notes("Provides information about the multicluster configuration.").
Operation("getMulticlusterConfiguration").
To(h.getConfigz))
webservice.Route(webservice.GET("/configs/theme").
Doc("Retrieve theme configurations").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagPlatformConfigurations}).
Notes("Retrieve theme configuration settings.").
Operation("get-theme-config").
To(h.getThemeConfiguration))
if h.config.MultiClusterOptions.ClusterRole == string(clusterv1alpha1.ClusterRoleHost) {
webservice.Route(webservice.GET("/configs/oauth").
Doc("Retrieve OAuth configuration").
Notes("Provides information about the authorization server.").
Operation("getOAuthConfiguration").
To(h.getOAuthConfiguration))
webservice.Route(webservice.POST("/configs/theme").
Doc("Update theme configurations").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagPlatformConfigurations}).
Notes("Update theme configuration settings.").
Operation("update-theme-config").
To(h.updateThemeConfiguration))
webservice.Route(webservice.GET("/configs/theme").
Doc("Retrieve the current theme configuration").
Notes("Provides the current theme configuration details.").
Operation("getThemeConfiguration").
To(h.getThemeConfiguration))
webservice.Route(webservice.PUT("/configs/theme").
Doc("Update the theme configuration settings").
Notes("Allows the user to update the theme configuration settings.").
Operation("updateThemeConfiguration").
To(h.updateThemeConfiguration))
webservice.Route(webservice.POST("/platformconfigs").
Doc("Create a new platform configuration").
Notes("Allows the user to create a new configuration for the specified platform.").
Operation("createPlatformConfiguration").
To(h.createPlatformConfiguration))
webservice.Route(webservice.DELETE("/platformconfigs/{config}").
Doc("Delete the specified platform configuration").
Notes("Allows the user to delete the configuration settings of the specified platform.").
Operation("deletePlatformConfiguration").
To(h.deletePlatformConfiguration))
webservice.Route(webservice.PUT("/platformconfigs/{config}").
Doc("Update the specified platform configuration settings").
Notes("Allows the user to modify the configuration settings of the specified platform").
Operation("updatePlatformConfiguration").
To(h.updatePlatformConfiguration))
webservice.Route(webservice.PATCH("/platformconfigs/{config}").
Doc("Patch the specified platform configuration settings").
Consumes(runtime.MimeMergePatchJson).
Notes("Allows the user to apply partial modifications to the configuration settings of the specified platform").
Operation("patchPlatformConfiguration").
To(h.patchPlatformConfiguration))
webservice.Route(webservice.GET("/platformconfigs/{config}").
Doc("Retrieve the specified platform configuration").
Notes("Provides details of the specified platform configuration.").
Operation("getPlatformConfiguration").
To(h.getPlatformConfiguration))
}
for _, route := range webservice.Routes() {
route.Metadata = map[string]interface{}{
restfulspec.KeyOpenAPITags: []string{api.TagPlatformConfigurations},
}
}
c.Add(webservice)
return nil