Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement issueops with faas

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use faas to achieve issueops", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use faas to achieve issueops" bar!

Issueops?

Chatops,gitops has not heard of issueops. As the name implies, issueops does the ops thing when discussing issue, and it is beautiful to solve the problem when discussing the problem.

Things like this are often seen in kubernetes projects like issue or PR:

Instructions like / kind feature are for robots, so here's a very diligent buddy:

His name is k8s-ci-robot. I don't think anyone has contributed as much as him. This is not a brush, but a real workload:

After receiving the instruction, robot is busy tagging to verify whether the person asking the question has cla authentication, the person who assigned the review code, and so on:

Then the slackers review the code and tell the robot to test:

The little friend did a lot of things like a tiger without winning the year-end bonus:

Above, do enterprises find that raising an robot is better than recruiting 10 employees, which is much more reliable than programming programmers for weekly reports during the epidemic?

Prow

Prow is the implementation of robot, the principle is very simple, that is, through github webhook to listen to the events generated by github, analyze the instructions inside to execute the corresponding job, it can do several key things:

Perform tasks, especially test tasks

Merge the code, you might think it's just a matter of clicking a button? In fact, no, for example, a bug fix may be merged into many versions, which is boring and disgusting, and most of the time, you still want to do some extra things when merging code, such as notification, tagging and so on.

Parsing and executing instructions like / foo, which is very important, basically do whatever you want.

A front end is used to display merge status, tasks, etc.

The steps for Prow deployment are as follows:

Apply for a github account for robots. A separate account is recommended.

There is a K8s cluster.

Configure permissions and create token for secret to store github accounts. To call github SDK, you need to use

Start prow and you will get an address of http

Configured in your github repository, webhook points to the prow address

Do not forget that we are very poor, isolation at home will soon be out of work, the server can not afford to buy K8s cluster? So what's the use of talking so much about prow!

Next, whoring mode is turned on.

We need to solve two problems:

Free computing resources run the http service and call the github webhook

A platform to run job for free

Free httpserver

How to find a free http service? We naturally turn our vicious eyes to function computation:

Aliyun Tencent Cloud has a free quota, and this amount is enough for a small task like mine. I have a bad conscience.

FaaS

It's a pity that Aliyun doesn't have a default golang function template, but it supports a custom environment, as long as it listens to port 9000.

For me who despise UI, I can't afford to lose this person to create a function on the page. The command line tool fun must be used:

Function configuration information

Template.yml:

ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: CRService: Type: 'Aliyun::Serverless::Service' Properties: Description:' custom runtime demo' LogConfig: # Log configuration, you need to communicate with CLS This piece is not very good for novice experience Project: 'sealyun' Logstore:' robot' Policies:-AliyunLogFullAccess robot: Type: 'Aliyun::Serverless::Function' Properties: Handler: index.handler CodeUri:. / code.zip Description:' demo with custom runtime' Runtime: custom Events: http_t : Type: HTTP Properties: AuthType: ANONYMOUS Methods: ['GET' 'POST', 'PUT',' DELETE', 'HEAD'

In the code framework, body is the events message sent from github:

Func handler (w http.ResponseWriter, req * http.Request) {bMagne err: = ioutil.ReadAll (req.Body) event: = & issue.IssueCommentEvent {}...} func main () {fmt.Println ("FunctionCompute go runtime inited.") Http.HandleFunc ("/", handler) port: = os.Getenv ("FC_SERVER_PORT") if port = "" {port = "9000"} http.ListenAndServe (":" + port, nil)}

Write a simple script to deploy the function:

When the function starts, only bootstrap binaries are recognized and packaged in zip format. Some accesskey-related configurations will be prompted when fun deploy.

Go build-o bootstrap faas.go zip code.zip bootstrap fun deploy

After deployment, there is an address in your trigger:

Configure this address in github webhook

This basic problem is solved. It should be noted that the configuration of the function calculation log is a bit small, which will not be mentioned in detail here.

Perform a task

If the custom environment for function computing is strong enough, then we can directly perform some tasks in the function, such as compilation, testing, etc., but this is not very friendly and unrealistic, the unfriendly place is that the way of customizing the environment is too simple, for example, if you need to call the git command in the function, then you can only pack it together in zip, not as flexible as Dockerfile, and did not find which is compatible with Dockerfile standards. In addition, there are too many packaged things. It is possible that the cold start of the function will be slow.

Similarly, I also hope that when implementing some functions, I do not need to change the code of the robot, but only need to modify some external configurations or scripts to achieve different tasks. In this way, drone is coming.

Drone promote event

Let's first introduce the drone promote event, which allows us to trigger an action in pipeline through http. This usage scenario is very extensive, a typical scenario: our project needs to be online after compilation and release, and there are two environments, development environment and test environment.

Pipeline configuration:

-name: dev image: golang:1.12 commands:-echo "deploy to development environment" when: event:-promote target:-dev-name: test image: golang:1.12 commands:-echo "deploy to test environment" when: event:-promote target:-test

So we want to deploy only in the development environment first, then execute the following command:

# drone build promote Project build number Target drone build promote fanux/sealos 42 dev

In this way, the dev step will be performed, but the test will not.

If you are an open source project, you can use the public services provided by drone for free and get a good cloud.drone.io.

Drone promote docking FaaS

Since the promote event is triggered, then just download the drone command line, why listen to the event and call SDK to circle around? This brings me back to the original topic: I hope to solve the problem when discussing the problem.

I also like to put everything in the cloud. I don't want to install the drone client locally at all, and most of the time I can call the shots on my phone. And I also hope to reply to the issue execution result and so on after execution.

You can use the Drone official sdk here:

/ / create clientconfig: = new (oauth3.Config) auther: = config.Client (context.Background (), & oauth3.Token {AccessToken: d.DroneToken,},) client: = drone.NewClient (d.DroneServer, auther) / / trigger promote event _, err: = client.Promote (namespace,name,cmd.Build,cmd.Target,cmd.Params)

Namespace is the github user name or group name, and name is the repository name. For example, the drone/drone-go project drone is namespace drone-go, and name Params is a parameter of key value, which is injected into the environment variable of pipeline, which is also very useful.

Write the above into FaaS and you will have a prototype.

Final result: reply to issue

Pipeline is executed

Robot framework

Of course, I want robot to be as scalable as possible, and to be able to dock not only with drone but also with other systems and other instructions. So I've written a framework: robot.

You only need to write a specific processor for a specific command, and then register with the framework:

Type Robot interface {Process (event IssueEvent) error}

Note that the processor issue.Regist (command, processor) must be registered for Processor to take effect.

Take the drone promote processor as an example:

/ / the structure contains the drone server address and tokentype DronePromote struct {DroneServer string DroneToken string} / / implement the processor interface func (d * DronePromote) Process (event issue.IssueEvent) error {.}

Here event parses the commands in issue and throws them to your processor:

Type IssueEvent struct {* IssueCommentEvent Command * Command Client * github.Client}

You just need to care about what instructions your processor is dealing with, such as when using a promote processor:

/ / github sends the event data in json format, which has been parsed to func promote (ctx context.Context, event issue.IssueCommentEvent) (string, error) {/ / or using env: GITHUB_USER GITHUB_PASSWD / / github account name and password in the event structure, because the robot may have to reply to issue or something. It is recommended to apply for a separate account for the robot / / read config: = issue.NewConfig ("sealrobot", "xxx") / / regist what robot your need from the environment variable without passing parameters, and the robot config / / register which robot you want to process, because there may be special instructions in an issue All we care about is / promote / / the processor of Drone needs to know the address of drone and what token is issue.Regist ("/ promote", & drone_promote.DronePromote {"https://cloud.drone.io"," QS3SmhZVpJAmb7tWPuWIOh4BhuI "}) / / process issue err: = issue.Process (config, event) return fmt.Sprintf (" goversionecho% s ", err), nil}

Register your processor with Regist so that the processor will only handle "/ promote..." in issue. Instructions for:

Issue.Regist ("/ promote", & drone_promote.DronePromote {"https://cloud.drone.io"," QS3SmhZVpJAmb7tWPuWIOh4BhuI "}) Thank you for your reading. The above is the content of" how to use faas to achieve issueops ". After the study of this article, I believe you have a deeper understanding of how to use faas to achieve issueops, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report