Introduction

KusionStack is an open-source one-stop programmable protocol stack for IaC configuration coding practice for Kubernetes cloud-native scenarios. The basic idea is to allow students from the "application side + platform side" to work together on the same plane of the Konfig model library built on IaC for DevOps collaboration. Today we share with you a fun Go code city application and how KusionStack deploys it to the K8s cloud native environment with one click.

The main repository of the KusionStack project:

https://github.com/KusionStack/kusion

PART. 1

What is CodeCity

CodeCity is a creative application developed by Swedish engineer Richard Wettel that presents and measures the complexity of code in a digital city-like visual format. Its effect is shown in the figure:

The central landmarks in Code City in 3D are very intuitive - the largest and tallest buildings are always the focal point for easy tracking. Because of this unique idea, CodeCity won the first prize of "Riconoscimento ated-ICT Ticino" in 2008, and it is also free for non-commercial research and learning purposes.

The GoCity to be shown today is the Go language version of Code City, and in this way we can evaluate the code complexity of Go language projects such as KusionStack. You can also view the KusionStack/kusion repositories through the online GoCity.

PART. 2

Execute GO code city locally

The previous GoCity was updated in October 2021. There were some minor problems in the latest Docker and Go1.18 environments. Fortunately, KusionStack related students submitted patches to fix it (_ This is also the charm of open source projects, and also I hope that the open source community partners can participate in the co-construction of KusionStack_), now you can execute the following command to install: go install github.com/rodrigo-brito/gocity@latest. Then open Github or local repository via gocity open.

- For example, open the local KusionStack/kusion repository

- Then the browser opens the corresponding page

Local execution everything works fine!

PART. 3

Go code city one-click cloud access

As an application similar to a digital city, in the context of cloud native and metaverse, deployment to the cloud is also a natural requirement. At the same time, we also hope to show the basic usage of KusionStack through GoCity. Before GoCity goes to the cloud, let's try out how to execute the application locally.

The corresponding container image has been pushed to Docker Hub https://hub.docker.com/r/yuanhao1223/gocity , run the command as follows:

After running successfully, you can open the local address http://localhost:4000/ to view the digital city 3D effect of the Go project.

After successful containerization, it is now ready to go to the cloud. It can be seen from the way that containers are executed locally that to deploy in Kubernetes, at least two resources, Deployment and Service, are required. The Deployment is used to deploy the Go code city, and the Service exposes the port to access stateless applications.

First, refer to the installation documentation https://kusionstack.io/docs/user_docs/getting-started/install/ to install the local Kusion command, and then provide the corresponding template through the online repository of kusion init. The Kusion command supports one-click initialization configuration:

The output is similar to the following:

For ease of presentation, Kusion templates have built-in CodeCity examples. The code-city template depends on the abstract front/back-end model in the konfig library, and the code-city template has no dependencies and can self-close the loop. We choose the latter:

During the initialization process, the container image is specified, and the container port and service port are both 4000. Now enter the configuration directory, and the directory structure is as follows:

- The complete code can refer to:

https://github.com/KusionStack/kusion-templates/tree/main/code-city-demo

In order to facilitate local testing, the MiniKube service can be started locally through minikube start. Then switch the command line mode to the code-city-kcl directory, and then execute the kusion apply command to take effect. At this point, the official cloud start:
kusion apply main.k

The output is similar to:

Check the status of the Deployment:

The output is similar to:

Use kubectl port forwarding:

Visit the local address https://localhost:4000/) , click the link "KusionStack/kusion" at Example, you can see the same effect as the local execution:

So far, the one-click cloud migration of Go Code City has been completed. Interested readers can choose other templates based on the model library Konfig to explore other operation and maintenance scenarios supported by KusionStack. Next, we will explore the internal principles of Code City.

PART. 4

Recognize architectural implications in digital cities

To be honest, Code City looks more like a circuit board at first glance. To understand its meaning, you need to understand several basic parameter mapping relationships, as shown in the lower right corner of the preview page:

The above correspondence is also explained in its official documentation, as shown in the following figure:

The pink on the ground represents the directory corresponding to the Go package (_because the dependencies of the package may be superimposed_), the gray represents the files inside the directory, and the blue represents the structure. The size of the gray building that represents the file is the size of the file, the height of the blue building that represents the structure is the number of methods, the length and width of the building represents the number of attributes in the structure, and the depth of the blue color represents the relevant code Rows.

We can select the DiffOptions structure corresponding to the building to view its related property parameters:

You can see that there are 15 properties, 3 methods, and a total of 156 lines of code in this structure. You can jump to the corresponding location by clicking the "Github link" button in it:

Therefore, in this way, we can easily check whether there are particularly tall buildings globally, so as to determine whether there are certain files and structures that need to be improved. It can be said that GoCity is a very interesting code analysis tool, and can even be integrated into the Github PR code review process.

PART. 5

Analyze GoCity's code architecture

The GoCity code architecture is mainly divided into two parts: code data extraction and front-end model display, as shown in the figure:

First, Codebase represents the code to be displayed, which is pulled through Git Service, and then the corresponding parameter information is extracted through Parser and Position services, and then displayed through the front end. The Go language code is mainly concentrated in the model data extraction part, and the front-end display is mainly implemented by JS. The front-end display resource file is embedded into the program through embed.FS, and the GoCity command starts the Web service display page. The code structure is relatively clear, and it is also an ideal open source project for Go language learning.

PART. 6

Outlook

Through KusionStack, with a small amount of KCL configuration code, we have completed the one-click cloud operation of Go Code City. While there is no discernible difference between the Go Code City on the cloud and the local version, the overall lifecycle management of the program on the cloud will be very different. In the following example, we will show how to use KusionStack combined with the KCL configuration language to perform the operation and maintenance of cloud-native applications in the IaC mode. Thanks for following 🙏

Reference link

https://github.com/KusionStack/kusion

https://github.com/KusionStack/examples

https://github.com/rodrigo-brito/gocity

https://wettel.github.io/codecity.html

understand more...

KusionStack Star ✨:

https://github.com/KusionStack/Kusion

Recommended reading of the week

The exploration and practice of KusionStack in Ant Group (Part 1)

Exploration and practice of Kusion model library and tool chain

Wonderful Review|KusionStack

It took two years to break the predicament of "interlaced like mountains"

Welcome to scan the code and follow our official account


SOFAStack
426 声望1.6k 粉丝

SOFAStack™(Scalable Open Financial Architecture Stack)是一套用于快速构建金融级分布式架构的中间件,也是在金融场景里锤炼出来的最佳实践。