未验证 提交 028656fd 编写于 作者: A alex.fan 提交者: GitHub

Merge pull request #2 from kubesphere/master

fork code
......@@ -40,8 +40,11 @@ func handleGetComponents(request *restful.Request, response *restful.Response) {
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
}
response.WriteAsJson(result)
} else {
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) {
......@@ -38,16 +48,59 @@ func handlerRegistryValidation(request *restful.Request, response *restful.Respo
err := request.ReadEntity(&authinfo)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
} else {
result := models.RegistryLoginAuth(authinfo)
response.WriteAsJson(result)
}
}
func handleCreateRegistries(request *restful.Request, response *restful.Response) {
registries := models.Registries{}
err := request.ReadEntity(&registries)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
}
result := models.RegistryLoginAuth(authinfo)
result, err := models.CreateRegistries(registries)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
} else {
response.WriteAsJson(result)
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)
}
}
\ No newline at end of file
......@@ -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"
"strings"
"time"
"github.com/golang/glog"
"strings"
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
}
}
\ No newline at end of file
......@@ -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,
}
}
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)
Message string `json:"message"`
Reason string `json:"reason"`
}
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
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册