Skip to content

Commit

Permalink
feature: bcs-client & bcs-api support yaml, issue TencentBlueKing#362
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperJim committed Feb 25, 2020
1 parent 0197fd3 commit f1574a8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func request2mesosapi(req *restful.Request, uri, method string) (string, error)
return err1.Error(), nil
}
//check application media type
if req.Request.Header.Get(medieTypeHeader) == mediaTypeApplicationYaml {
if mediaTypeApplicationYaml == req.Request.Header.Get(medieTypeHeader) {
data, err = yamlTOJSON(data)
if err != nil {
blog.Errorf("bcs-api handle url %s yaml to json failed, %s", uri, err.Error())
mediaErr := bhttp.InternalError(common.BcsErrApiMediaTypeError, common.BcsErrApiMediaTypeErrorStr)
return mediaErr.Error(), nil
} else {
blog.V(3).Infof("bcs-api handle url %s converting yaml to json successfully", uri)
blog.V(3).Infof("bcs-api handle url %s converting yaml to json successfully", uri)
}
}

Expand Down
17 changes: 9 additions & 8 deletions bcs-services/bcs-client/cmd/batch/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ func apply(cxt *utils.ClientContext) error {
//step: check json object from parsing
info.apiVersion, info.kind, err = metaList.GetResourceKind()
if err != nil {
fmt.Printf("apply partial failed, %s, continue...", err.Error())
fmt.Printf("apply partial failed, %s, continue...\n", err.Error())
continue
}
info.namespace, info.name, err = metaList.GetResourceKey()
if err != nil {
fmt.Printf("apply partial failed, %s, continue...", err.Error())
fmt.Printf("apply partial failed, %s, continue...\n", err.Error())
continue
}
info.rawJson = metaList.GetRawJSON()
utils.DebugPrintf("debugInfo: %s\n", string(info.rawJson))
info.clusterID = cxt.ClusterID()
//step: inspect resource object from storage
// if resource exist, update resource to bcs-scheduler, print object status from response
Expand Down Expand Up @@ -163,7 +164,7 @@ func apply(cxt *utils.ClientContext) error {
//unkown type, try custom resource
crdapiVersion, plural, crdErr := utils.GetCustomResourceTypeByKind(scheduler, cxt.ClusterID(), info.kind)
if err != nil {
fmt.Printf("resource %s/%s %s apply failed, %s.", info.apiVersion, info.kind, info.name, crdErr.Error())
fmt.Printf("resource %s/%s %s apply failed, %s\n", info.apiVersion, info.kind, info.name, crdErr.Error())
continue
}
_, inspectStatus = scheduler.GetCustomResource(cxt.ClusterID(), crdapiVersion, plural, info.namespace, info.name)
Expand All @@ -188,21 +189,21 @@ func applySpecifiedResource(inspectStatus error, create createFunc, update updat
if inspectStatus == nil {
//update object
if err := update(info.clusterID, info.namespace, info.rawJson, nil); err != nil {
fmt.Printf("resource %s/%s %s update successfully.", info.apiVersion, info.kind, info.name)
fmt.Printf("resource %s/%s %s update successfully\n", info.apiVersion, info.kind, info.name)
} else {
fmt.Printf("resource %s/%s %s update failed, %s.", info.apiVersion, info.kind, info.name, err.Error())
fmt.Printf("resource %s/%s %s update failed, %s\n", info.apiVersion, info.kind, info.name, err.Error())
}
return
}

if !isObjectNotExist(inspectStatus) {
fmt.Printf("resource %s/%s %s apply failed, %s.", info.apiVersion, info.kind, info.name, inspectStatus.Error())
fmt.Printf("resource %s/%s %s apply failed, %s\n", info.apiVersion, info.kind, info.name, inspectStatus.Error())
return
}
//create
if err := create(info.clusterID, info.namespace, info.rawJson); err != nil {
fmt.Printf("resource %s/%s %s create failed, %s.", info.apiVersion, info.kind, info.name, err.Error())
fmt.Printf("resource %s/%s %s create failed, %s\n", info.apiVersion, info.kind, info.name, err.Error())
} else {
fmt.Printf("resource %s/%s %s create successfully", info.apiVersion, info.kind, info.name)
fmt.Printf("resource %s/%s %s create successfully\n", info.apiVersion, info.kind, info.name)
}
}
14 changes: 7 additions & 7 deletions bcs-services/bcs-client/cmd/batch/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func clean(cxt *utils.ClientContext) error {
//step: check json object from parsing
info.apiVersion, info.kind, err = metaList.GetResourceKind()
if err != nil {
fmt.Printf("apply partial failed, %s, continue...", err.Error())
fmt.Printf("apply partial failed, %s, continue...\n", err.Error())
continue
}
info.namespace, info.name, err = metaList.GetResourceKey()
if err != nil {
fmt.Printf("apply partial failed, %s, continue...", err.Error())
fmt.Printf("apply partial failed, %s, continue...\n", err.Error())
continue
}
//info.rawJson = metaList.GetRawJSON()
Expand Down Expand Up @@ -142,7 +142,7 @@ func clean(cxt *utils.ClientContext) error {
//unkown type, try custom resource
crdapiVersion, plural, crdErr := utils.GetCustomResourceTypeByKind(scheduler, cxt.ClusterID(), info.kind)
if err != nil {
fmt.Printf("resource %s/%s %s clean failed, %s.", info.apiVersion, info.kind, info.name, crdErr.Error())
fmt.Printf("resource %s/%s %s clean failed, %s\n", info.apiVersion, info.kind, info.name, crdErr.Error())
continue
}
_, inspectStatus = scheduler.GetCustomResource(cxt.ClusterID(), crdapiVersion, plural, info.namespace, info.name)
Expand All @@ -159,15 +159,15 @@ func cleanSpecifiedResource(inspectStatus error, delfunc deleteFunc, info *metaI
if inspectStatus == nil {
//no error when inspect, it means data exist
if err := delfunc(info.clusterID, info.namespace, info.name, false); err != nil {
fmt.Printf("resource %s/%s %s clean failed, %s.", info.apiVersion, info.kind, info.name, err.Error())
fmt.Printf("resource %s/%s %s clean failed, %s\n", info.apiVersion, info.kind, info.name, err.Error())
} else {
fmt.Printf("resource %s/%s %s clean successfully", info.apiVersion, info.kind, info.name)
fmt.Printf("resource %s/%s %s clean successfully\n", info.apiVersion, info.kind, info.name)
}
return
}
if isObjectNotExist(inspectStatus) {
fmt.Printf("resource %s/%s %s clean nothing", info.apiVersion, info.kind, info.name)
fmt.Printf("resource %s/%s %s clean nothing\n", info.apiVersion, info.kind, info.name)
} else {
fmt.Printf("resource %s/%s %s clean failed, %s", info.apiVersion, info.kind, info.name, inspectStatus.Error())
fmt.Printf("resource %s/%s %s clean failed, %s\n", info.apiVersion, info.kind, info.name, inspectStatus.Error())
}
}
8 changes: 7 additions & 1 deletion bcs-services/bcs-client/pkg/metastream/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const (
YAMLFormat = "yaml"
)

type metaObject struct {
mesostype.ObjectMeta `json:"metadata,omitempty"`
}

//NewJSONStream create stream implementation
func NewMetaStream(r io.Reader, ft string) Stream {
allDatas, err := ioutil.ReadAll(r)
Expand All @@ -65,9 +69,11 @@ func NewMetaStream(r io.Reader, ft string) Stream {
if YAMLFormat == ft {
newJSON, err := yaml.YAMLToJSON([]byte(newLine))
if err != nil {
fmt.Printf("yaml convert err, %s\n", err.Error())
continue
}
newLine = string(newJSON)
//fmt.Printf("original yaml convert to json: %s\n", newLine)
}
clearList = append(clearList, newLine)
}
Expand Down Expand Up @@ -116,7 +122,7 @@ func (js *jsonArray) GetResourceKind() (string, string, error) {

//GetResourceKey return JSON object index: namespace & name
func (js *jsonArray) GetResourceKey() (string, string, error) {
objMeta := &mesostype.ObjectMeta{}
objMeta := &metaObject{}
err := json.Unmarshal([]byte(js.indexRawJson), objMeta)
if err != nil {
return "", "", fmt.Errorf("json %d object err: %s", js.index-1, err.Error())
Expand Down

0 comments on commit f1574a8

Please sign in to comment.