Skip to content

Commit

Permalink
Improves logging
Browse files Browse the repository at this point in the history
Signed-off-by: Darkhood148 <[email protected]>
  • Loading branch information
Darkhood148 committed Feb 15, 2025
1 parent 65ee4de commit 67814c4
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 198 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ lib/harbor/certs/*
peer_configs/*
work.txt
.DS_Store
katana-services/*
katana-services/*
.idea/
2 changes: 1 addition & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ country = "IN"
province = "Delhi"
locality = "Delhi"
street_address = "smoking jawahar"
postal_code = "110080"
postal_code = "110080"
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/mholt/archiver/v3 v3.5.1
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/rs/zerolog v1.33.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
Expand All @@ -33,8 +34,6 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/caddyserver/certmagic v0.21.2 // indirect
github.com/caddyserver/zerossl v0.1.3 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down Expand Up @@ -71,16 +70,12 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/libdns/libdns v0.2.2 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mholt/acmez/v2 v2.0.1 // indirect
github.com/miekg/dns v1.1.59 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.5.0 // indirect
Expand Down Expand Up @@ -113,10 +108,7 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
Expand Down
57 changes: 18 additions & 39 deletions go.sum

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions lib/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/sdslabs/katana/logging"
"io"
"log"
"path/filepath"
Expand All @@ -26,6 +27,8 @@ import (
"k8s.io/client-go/restmapper"
)

var logger = &logging.GlobalLogger

// ApplyManifest applies a given manifest to the cluster
func ApplyManifest(kubeconfig *rest.Config, kubeclientset *kubernetes.Clientset, manifest []byte, namespace string) error {
dd, err := dynamic.NewForConfig(kubeconfig)
Expand Down Expand Up @@ -123,7 +126,7 @@ func DeployCluster(kubeconfig *rest.Config, kubeclientset *kubernetes.Clientset)

for _, m := range clusterConfig.TemplatedManifests {
manifest := &bytes.Buffer{}
log.Printf("Applying: %s\n", m)
logger.Info().Msgf("Applying: %s\n", m)
tmpl, err := template.ParseFiles(filepath.Join(clusterConfig.TemplatedManifestDir, m))
if err != nil {
return err
Expand All @@ -135,7 +138,7 @@ func DeployCluster(kubeconfig *rest.Config, kubeclientset *kubernetes.Clientset)
if err = ApplyManifest(kubeconfig, kubeclientset, manifest.Bytes(), g.KatanaConfig.KubeNameSpace); err != nil {
return err
}

}

return nil
Expand All @@ -153,21 +156,21 @@ func DeployChallengeToCluster(challengeName, teamName string, firstPatch bool, r
/// Retrieve the existing deployment
existingDeployment, err := deploymentsClient.Get(context.TODO(), challengeName, metav1.GetOptions{})
if err != nil {
log.Println("Error in retrieving existing deployment.")
log.Println(err)
logger.Error().Msgf("Error in retrieving existing deployment.")
logger.Error().Err(err)
return err
}

existingDeployment.Spec.Template.Spec.Containers[0].Image = "harbor.katana.local/katana/" + teamName + "-" + challengeName + ":latest"

_, err = deploymentsClient.Update(context.TODO(), existingDeployment, metav1.UpdateOptions{})
if err != nil {
log.Println("Error in updating deployment.")
log.Println(err)
logger.Error().Msgf("Error in updating deployment.")
logger.Error().Err(err)
return err
}

log.Println("Updated deployment with new image.")
logger.Info().Msgf("Updated deployment with new image.")
return nil
}

Expand Down Expand Up @@ -219,7 +222,6 @@ func DeployChallengeToCluster(challengeName, teamName string, firstPatch bool, r
return nil
}


func DeployChallengeCheckerToCluster(challengeCheckerName, namespace string, replicas int32) error {

kubeclient, _ := utils.GetKubeClient()
Expand All @@ -230,7 +232,7 @@ func DeployChallengeCheckerToCluster(challengeCheckerName, namespace string, rep
manifest := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: challengeCheckerName+"-deployment",
Name: challengeCheckerName + "-deployment",
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Expand Down Expand Up @@ -264,16 +266,15 @@ func DeployChallengeCheckerToCluster(challengeCheckerName, namespace string, rep
},
},
}
log.Println("Creating deployment...")

logger.Info().Msgf("Creating deployment...")
result, err := deploymentsClient.Create(context.TODO(), manifest, metav1.CreateOptions{})

if err != nil {
log.Println("Unable to create deployment")
logger.Error().Msgf("Unable to create deployment")
panic(err)
}

log.Printf("Created deployment %q.\n", result.GetObjectMeta().GetName()+" in namespace "+namespace)
logger.Info().Msgf("Created deployment %q.\n", result.GetObjectMeta().GetName()+" in namespace "+namespace)
return nil
}

3 changes: 3 additions & 0 deletions lib/harbor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/sdslabs/katana/logging"
"net/http"

config "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/lib/utils"
)

var logger = &logging.GlobalLogger

var baseURL string = "https://harbor.katana.local/api/v2.0"

var httpClient *http.Client = &http.Client{
Expand Down
4 changes: 1 addition & 3 deletions lib/harbor/setup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package harbor

import (
"log"

"github.com/sdslabs/katana/lib/utils"
)

Expand All @@ -19,7 +17,7 @@ func SetupHarbor() error {

for _, deploymentName := range deploymentNames {
if err := utils.WaitForDeploymentReady(client, deploymentName, namespace); err != nil {
log.Printf("Error testing deployment '%s': %v\n", deploymentName, err)
logger.Error().Msgf("Error testing deployment '%s': %v\n", deploymentName, err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/mysql/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"database/sql"
"fmt"
"github.com/sdslabs/katana/logging"
"log"
"time"

Expand All @@ -13,6 +14,7 @@ import (
)

var db *sql.DB
var logger = &logging.GlobalLogger

func setup() error {
for i := 0; i < 10; i++ {
Expand Down
6 changes: 3 additions & 3 deletions lib/mysql/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func CreateGogsUser(username, password string) error {
createdTime := time.Now().Unix()
rand, err := utils.RandomSalt()
if err != nil {
log.Println(err)
logger.Error().Err(err)
}

salt, err := utils.RandomSalt()
if err != nil {
log.Println(err)
logger.Error().Err(err)
}

password = utils.EncodePassword(password, salt)
Expand All @@ -58,7 +58,7 @@ func CreateGogsUser(username, password string) error {
query := "INSERT INTO `user` (`id`, `lower_name`, `name`, `full_name`, `email`, `passwd`, `login_source`, `login_name`, `type`, `location`, `website`, `rands`, `salt`, `created_unix`, `updated_unix`, `last_repo_visibility`, `max_repo_creation`, `is_active`, `is_admin`, `allow_git_hook`, `allow_import_local`, `prohibit_login`, `avatar`, `avatar_email`, `use_custom_avatar`, `num_followers`, `num_following`, `num_stars`, `num_repos`, `description`, `num_teams`, `num_members`) VALUES (NULL, '" + user.LowerName + "', '" + user.Name + "', '" + user.FullName + "', '" + user.Email + "', '" + user.Password + "', '0', '', '0', '', '', '" + user.Rands + "', '" + user.Salt + "', '" + strconv.FormatInt(user.CreatedUnix, 10) + "', '" + strconv.FormatInt(user.UpdatedUnix, 10) + "', '0', '-1', '1', '0', '0', '0', '0', '" + user.Avatar + "', '" + user.AvatarEmail + "', '0', '0', '0', '0', '0', '', '0', '0')"
_, err = gogs.Exec(query)
if err != nil {
log.Println(err)
logger.Error().Err(err)
}
return nil
}
Expand Down
36 changes: 18 additions & 18 deletions lib/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (

func dockerLogin(username string, password string) {

log.Println("Logging into Harbor, Please wait...")
logger.Info().Msgf("Logging into Harbor, Please wait...")

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatal("Error creating Docker client:", err)
logger.Fatal().Err(err)
return
}

Expand All @@ -32,10 +32,10 @@ func dockerLogin(username string, password string) {

_, err = cli.RegistryLogin(context.Background(), authConfig)
if err != nil {
log.Printf("Error during login: %s\n", err)
logger.Fatal().Msgf("Error during login: %s\n", err)
return
}
log.Println("Logged into Harbor successfully")
logger.Info().Msgf("Logged into Harbor successfully")
}

func CheckDockerfile(_DockerfilePath string) bool {
Expand All @@ -46,19 +46,19 @@ func CheckDockerfile(_DockerfilePath string) bool {
func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {
buf := new(bytes.Buffer)
if err := Tar(_DockerfilePath, buf); err != nil {
log.Fatal(err, ": error tarring directory")
logger.Fatal().Err(err)
return
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatal(err)
logger.Fatal().Err(err)
return
}

log.Println(_ChallengeName)
log.Println(_DockerfilePath)
logger.Debug().Msgf(_ChallengeName)
logger.Debug().Msgf(_DockerfilePath)

log.Println("Building Docker image, Please wait......")
logger.Info().Msgf("Building Docker image, Please wait......")

imageBuildResponse, err := cli.ImageBuild(
context.Background(),
Expand All @@ -70,29 +70,29 @@ func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {
},
)
if err != nil {
log.Fatal(err, " :unable to create image")
logger.Fatal().Err(err)
return
}

_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
logger.Fatal().Err(err)
return
}

log.Println("Docker image built successfully")
logger.Info().Msgf("Docker image built successfully")

dockerLogin(configs.KatanaConfig.Harbor.Username, configs.KatanaConfig.Harbor.Password)

log.Println("Pushing Docker image to Harbor, please wait...")
logger.Info().Msgf("Pushing Docker image to Harbor, please wait...")

authConfig := registry.AuthConfig{
Username: configs.KatanaConfig.Harbor.Username,
Password: configs.KatanaConfig.Harbor.Password,
}
authJSON, err := json.Marshal(authConfig)
if err != nil {
log.Fatal(err, ": error encoding credentials")
logger.Fatal().Err(err)
return
}

Expand All @@ -104,15 +104,15 @@ func BuildDockerImage(_ChallengeName string, _DockerfilePath string) {

_, err = cli.ImagePush(context.Background(), "harbor.katana.local/katana/"+_ChallengeName, pushOptions)
if err != nil {
log.Fatal(err, " :unable to push docker image")
logger.Fatal().Err(err)
return
}

log.Println("Image Pushed to Harbor successfully")
logger.Info().Msgf("Image Pushed to Harbor successfully")

}

//this func is modified to pass args to dockerfile, so that name of challenge could be identified
// this func is modified to pass args to dockerfile, so that name of challenge could be identified
func BuildDockerImageCc(_ChallengeName string, _DockerfilePath string) {
buf := new(bytes.Buffer)
if err := Tar(_DockerfilePath, buf); err != nil {
Expand All @@ -134,7 +134,7 @@ func BuildDockerImageCc(_ChallengeName string, _DockerfilePath string) {
Dockerfile: "Dockerfile",
Remove: true,
Tags: []string{"harbor.katana.local/katana/" + _ChallengeName},
BuildArgs: map[string]*string{"chall_name":&_ChallengeName},
BuildArgs: map[string]*string{"chall_name": &_ChallengeName},
},
)
if err != nil {
Expand Down
Loading

0 comments on commit 67814c4

Please sign in to comment.