1. add registries create, query apis
2. fix components bug
This commit is contained in:
@@ -40,8 +40,11 @@ func handleGetComponents(request *restful.Request, response *restful.Response) {
|
||||
if err != nil {
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
} else {
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,13 @@ limitations under the License.
|
||||
package registries
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/filter/route"
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/filter/route"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
@@ -30,6 +32,14 @@ func Register(ws *restful.WebService, subPath string) {
|
||||
Consumes(restful.MIME_JSON).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
ws.Route(ws.POST(subPath).To(handleCreateRegistries).Filter(route.RouteLogging)).
|
||||
Consumes(restful.MIME_JSON).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
ws.Route(ws.GET(subPath + "/{project}").To(handleQueryRegistries).Filter(route.RouteLogging)).
|
||||
Consumes(restful.MIME_JSON).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
}
|
||||
|
||||
func handlerRegistryValidation(request *restful.Request, response *restful.Response) {
|
||||
@@ -42,12 +52,55 @@ func handlerRegistryValidation(request *restful.Request, response *restful.Respo
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
} else {
|
||||
|
||||
result := models.RegistryLoginAuth(authinfo)
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
result := models.RegistryLoginAuth(authinfo)
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
func handleCreateRegistries(request *restful.Request, response *restful.Response) {
|
||||
|
||||
registries := models.Registries{}
|
||||
|
||||
err := request.ReadEntity(®istries)
|
||||
|
||||
if err != nil {
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
}
|
||||
|
||||
result, err := models.CreateRegistries(registries)
|
||||
|
||||
if err != nil {
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
} else {
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handleQueryRegistries(request *restful.Request, response *restful.Response) {
|
||||
|
||||
project := request.PathParameter("project")
|
||||
result, err := models.QueryRegistries(project)
|
||||
|
||||
if err != nil {
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
} else {
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,11 +17,13 @@ limitations under the License.
|
||||
package models
|
||||
|
||||
import (
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"time"
|
||||
"github.com/golang/glog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
)
|
||||
|
||||
const KUBESYSTEM = "kube-system"
|
||||
@@ -66,7 +68,7 @@ func GetComponents() (result []Components, err error) {
|
||||
|
||||
var components Components
|
||||
|
||||
templates := [] string{"kube-apiserver", "etcd", "kube-scheduler", "kube-controller-manager", "cloud-controller-manager"}
|
||||
templates := []string{"kube-apiserver", "etcd", "kube-scheduler", "kube-controller-manager", "cloud-controller-manager"}
|
||||
|
||||
if len(podlists.Items) > 0 {
|
||||
|
||||
@@ -191,7 +193,7 @@ func GetComponents() (result []Components, err error) {
|
||||
components.Name = ds.Name
|
||||
components.Kind = "Daemonset"
|
||||
components.SelfLink = ds.SelfLink
|
||||
components.Label = ds.Labels
|
||||
components.Label = ds.Spec.Selector.MatchLabels
|
||||
components.Namespace = ds.Namespace
|
||||
version := strings.Split(ds.Spec.Template.Spec.Containers[0].Image, ":")
|
||||
|
||||
@@ -252,7 +254,7 @@ func GetComponents() (result []Components, err error) {
|
||||
components.Name = dm.Name
|
||||
components.Kind = "Deployment"
|
||||
components.SelfLink = dm.SelfLink
|
||||
components.Label = dm.Labels
|
||||
components.Label = dm.Spec.Selector.MatchLabels
|
||||
components.Namespace = dm.Namespace
|
||||
components.Replicas = int(dm.Status.Replicas)
|
||||
version := strings.Split(dm.Spec.Template.Spec.Containers[0].Image, ":")
|
||||
@@ -292,4 +294,4 @@ func GetComponents() (result []Components, err error) {
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
}
|
||||
@@ -17,14 +17,27 @@ limitations under the License.
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
kubeclient "kubesphere.io/kubesphere/pkg/client"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
const TYPE = "kubernetes.io/dockerconfigjson"
|
||||
|
||||
const SECRET = "Secret"
|
||||
|
||||
const APIVERSION = "v1"
|
||||
|
||||
type AuthInfo struct {
|
||||
Username string `json:"username"`
|
||||
@@ -32,13 +45,42 @@ type AuthInfo struct {
|
||||
ServerHost string `json:"serverhost"`
|
||||
}
|
||||
|
||||
type ValidationMsg struct {
|
||||
func NewAuthInfo(para Registries) *AuthInfo {
|
||||
|
||||
return &AuthInfo{
|
||||
Username: para.RegUsername,
|
||||
Password: para.RegPassword,
|
||||
ServerHost: para.RegServerHost,
|
||||
}
|
||||
|
||||
Message string `json:"message"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func convert2DockerJson(authinfo AuthInfo) []byte {
|
||||
|
||||
datastr := []byte(authinfo.Username + ":" + authinfo.Password)
|
||||
auth := base64.StdEncoding.EncodeToString(datastr)
|
||||
|
||||
dockercfg := fmt.Sprintf("{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"auth\":\"%s\"}}}",
|
||||
authinfo.ServerHost, authinfo.Username, authinfo.Password, auth)
|
||||
|
||||
return []byte(dockercfg)
|
||||
|
||||
}
|
||||
|
||||
type Registries struct {
|
||||
DisplayName string `json:"display_name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
AuthProject string `json:"auth_project,omitempty"`
|
||||
RegServerHost string `json:"reg_server_host,omitempty"`
|
||||
RegUsername string `json:"reg_username,omitempty"`
|
||||
RegPassword string `json:"reg_password,omitempty"`
|
||||
CreateUser string `json:"create_user,omitempty"`
|
||||
}
|
||||
|
||||
type ValidationMsg struct {
|
||||
Message string `json:"message"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
const DOCKERCLIENTERROR = "Docker client error"
|
||||
|
||||
@@ -90,4 +132,109 @@ func RegistryLoginAuth(authinfo AuthInfo) ValidationMsg {
|
||||
|
||||
}
|
||||
|
||||
//create registries
|
||||
func CreateRegistries(registries Registries) (msg constants.MessageResponse, err error) {
|
||||
|
||||
projects := strings.Split(registries.AuthProject, ",")
|
||||
|
||||
var secret v1.Secret
|
||||
|
||||
secret.Kind = SECRET
|
||||
secret.APIVersion = APIVERSION
|
||||
secret.Type = TYPE
|
||||
secret.Name = registries.DisplayName + ".key"
|
||||
|
||||
authinfo := NewAuthInfo(registries)
|
||||
data := make(map[string][]byte)
|
||||
data[".dockerconfigjson"] = convert2DockerJson(*authinfo)
|
||||
|
||||
secret.Data = data
|
||||
k8sclient := kubeclient.NewK8sClient()
|
||||
|
||||
labels := make(map[string]string)
|
||||
annotations := make(map[string]string)
|
||||
labels["app"] = "dockerhubkey"
|
||||
secret.Labels = labels
|
||||
|
||||
annotations["description"] = registries.Description
|
||||
annotations["createuser"] = registries.CreateUser
|
||||
secret.Annotations = annotations
|
||||
|
||||
for _, pro := range projects {
|
||||
|
||||
glog.Infof("create secret %s in %s ", registries.DisplayName, pro)
|
||||
_, err := k8sclient.CoreV1().Secrets(pro).Create(&secret)
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
} //end for
|
||||
|
||||
msg.Message = "success"
|
||||
|
||||
return msg, nil
|
||||
|
||||
}
|
||||
|
||||
//query registries host
|
||||
func QueryRegistries(project string) (regList []Registries, err error) {
|
||||
|
||||
k8sclient := kubeclient.NewK8sClient()
|
||||
|
||||
var options meta_v1.ListOptions
|
||||
options.LabelSelector = "app=dockerhubkey"
|
||||
|
||||
var reg Registries
|
||||
|
||||
secrets, err := k8sclient.CoreV1().Secrets(project).List(options)
|
||||
|
||||
if err != nil {
|
||||
|
||||
glog.Errorln(err)
|
||||
return regList, err
|
||||
|
||||
}
|
||||
|
||||
if len(secrets.Items) > 0 {
|
||||
|
||||
for _, secret := range secrets.Items {
|
||||
|
||||
reg.DisplayName = secret.Name
|
||||
reg.AuthProject = project
|
||||
|
||||
if err != nil {
|
||||
|
||||
glog.Errorln(err)
|
||||
return regList, err
|
||||
|
||||
}
|
||||
|
||||
var data map[string]interface{}
|
||||
err := json.Unmarshal(secret.Data[".dockerconfigjson"], &data)
|
||||
|
||||
if err != nil {
|
||||
|
||||
glog.Errorln(err)
|
||||
return regList, err
|
||||
|
||||
}
|
||||
|
||||
hostMap := data["auths"].(map[string]interface{})
|
||||
|
||||
for key, _ := range hostMap {
|
||||
|
||||
reg.RegServerHost = key
|
||||
|
||||
}
|
||||
|
||||
regList = append(regList, reg)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return regList, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user