This is the 18th day of my participation in the August Challenge

Requirements example 🚊

To solve each additional project, a new user, need to manually create a user to harbor, this is because in the early design of Kubernetes project deployment, project isolation, each project has its own namespace and user

Similar examples include automatic image cleaning, automatic new projects and so on

In order to complete the automation process, it is necessary to call the Harbor API to complete the corresponding functions

The installation

The official one-click installation script is provided, which can be divided into offline and online modes. It is recommended to use offline mode for slow access to docker Hub

Offline package: github.com/goharbor/ha…

Online package: github.com/goharbor/ha…

Download and decompress the package

tar xzvf harbor-offline-installer-v23.1..tgz
tar xzvf harbor-online-installer-v23.1..tgz
Copy the code

Run the one-click setup script

cd harbor-offline-installer
./install.sh
Copy the code

Access server IP, harbor default account password admin and Harbor12345

Harbor API 🚩

Harbor has already provided API interface and integrated Swagger document. Click Harbot API V2 in the lower left corner to open it

You can view the parameters and return values that need to be provided

Example of user query interface

  • Parameters: Provide a string username with optional page size and page number
  • Returned values: Username of type string and user_id of type int

Use the Try it Out button on the right of the page, then fill in the input field and hit Execute to get the result

API example ⛔

Curl curl curl curl curl curl curl curl curl curl curl curl

Create a user ⬆️

curl -u "admin:Harbor12345" -X POST -H "Content-Type: application/json" "https://harbor-test.xxxxx.org/api/v2.0/users" -d @user.json
Copy the code

user.json

{
  "user_id": 5."username": "zhangsan"."email": "[email protected]"."password": "Zs20210802"."realname": "zhangsan"."role_id": 2
}
Copy the code

Obtain user information ⬆️

curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor-test.xxxxx.org/api/v2.0/users"
Copy the code

Deleting a user ⬆️

curl -u "admin:Harbor12345" -X DELETE  -H "Content-Type: application/json" "Https://harbor-test.xxxxx.org/api/v2.0/users/ {user_id}"
Copy the code

Change the user password ⬆️

curl -u "admin:Harbor12345" -X PUT -H "Content-Type: application/json" "Https://harbor-test.xxxxx.org/api/v2.0/users/ {user_id} / password" -d @uppwd.json
# cat uppwd.json
{
  "old_password": "Harbor6666"."new_password": "Harbor8888"
}
Copy the code

Harbor the SDK 🚩

Although API can be called by cur command above and it is convenient to integrate into shell script, our whole Devops is project-oriented, and the automatic process uses golang language project, so golang is needed for implementation

Next, share the test results of the harbor SDK of two third parties. Finally, Goharp-Client can meet the needs of adding, deleting, modifying and checking, while Go-Harbor does not have the function of adding and deleting

Go to the SDK ❤ ️

Github can now find 2 go SDK packages github.com/mittwald/go… Github.com/TimeBye/go-… The Go-Harbor implements Projects Repositories Artifacts from three GROUPS of APIS. The others are not yet implemented

Goharbor-client is not detailed enough. It simply states that curD operation is supported. How can you call the test by yourself

Now call test for both packages

Go – harbor1 ️ ⃣

Use 1 ️

Now projects are using go mod to manage packages, all direct import can be synchronized

import "github.com/TimeBye/go-harbor"

Copy the code

Now let’s go through the APIS

  • The documentation states that there is no API for user, which there is
  • The implemented APIS currently support only three methods: Get,List, and Delete without creation and update functions
harborClient, err := harbor.NewClientSet("https://harbor-test.xxxxx.org"."username"."password")
iferr ! = nil { panic(err) }query := model.Query{}

/ / user
fmt.Println(harborClient.User.Get("1"))
fmt.Println(harborClient.User.List(&query))
fmt.Println(harborClient.User.Delete("6"))

/ / warehouse
fmt.Println(harborClient.V2.Repositories("bitsensor").Get("elastalert"))
fmt.Println(harborClient.V2.Repositories("cloudos").List(&query))
fmt.Println(harborClient.V2.Repositories("cloudos").Delete("cloudos-next-allinone"))

/ / project
fmt.Println(harborClient.V2.Get("15"))
fmt.Println(harborClient.V2.List(&query))
fmt.Println(harborClient.V2.Delete("11"))

/ / products
fmt.Println(harborClient.V2.Repositories("appsmith").Artifacts("appsmith-editor").List(&query))
fmt.Println(harborClient.V2.Repositories("appsmith").Artifacts("appsmith-editor").Get("latest"))
fmt.Println(harborClient.V2.Repositories("appsmith").Artifacts("appsmith-editor").Delete("latest"))
Copy the code

Partial result after run ✴️

For now, the project is just a wait and see project, because we don’t have the features we need right now

Goharbor – client2 ️ ⃣

This project has v1 and V2 versions, because our Harbor is 2.x and supports V2 version, the package of V2 version is directly imported here

import "github.com/mittwald/goharbor-client/v4/apiv2"
Copy the code

Since the project has no documentation (because it is based on the official Swagger UI), I had to explore by myself. However, after creating the client, I found that the project has very good semantics and all functions are very easy to find

Look at the picture

The API is attached to the URL that needs to be passed in when creating the client

harborClient, err := apiv2.NewRESTClientForHost("https://harbor-test.xxxxx.org/api/"."admin"."Harbor12345")
iferr ! = nil { log.Println(Harbor connection error,err)
}
Copy the code

Create user, warehouse, project examples

The following successful example is ✴️

harborClient, err := apiv2.NewRESTClientForHost("https://harbor-test.xxxxx.org/api/"."admin"."Harbor12345")
iferr ! = nil { log.Println(Harbor connection error,err)
}

// Create a user
result, err := harborClient.NewUser(context.TODO(),"zhangsan3"."[email protected]"."zhangsan3"."Zs20210803"."")
iferr ! = nil { log.Println("User creation error",err)
}
fmt.Println(result)

// Create a new project
var storageLimit int64 = 10
result2, err := harborClient.NewProject(context.TODO(),"zhangsan3",&storageLimit)
iferr ! = nil { log.Println("Error creating project",err)
}
fmt.Println(result2)

// Create a new warehouse
legacyModel := legacymodel.RegistryCredential{
   AccessKey: "".AccessSecret : "".Type: "",
}
result3,err := harborClient.NewRegistry(context.TODO(),"zhangsan3"."helm-hub"."http://ip:9000",&legacyModel,false)
iferr ! = nil { log.Println("Warehouse creation error",err)
}
fmt.Println(result3)
Copy the code

Return the result, although the create return error, you can see the project you just created on the screen

From the point of view of creation, this project has been able to meet our requirements, as for other interfaces, this article will not be posted one by one, interested colleagues can test it by themselves