Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add httpcall provider to processing events #190

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/api/GetHosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ package api
import (
"context"
"fmt"
"slices"

"github.com/maksim-paskal/kubernetes-manager/pkg/config"
"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -47,11 +47,11 @@ func (e *Environment) GetHosts(ctx context.Context) ([]string, []string, error)
for _, rule := range ingress.Spec.Rules {
host := fmt.Sprintf("%s://%s", *config.Get().IngressHostDefaultProtocol, rule.Host)
if ingress.Annotations != nil && ingress.Annotations[config.LabelType] == HostTypeInternal {
if !utils.StringInSlice(host, hostsInternal) {
if !slices.Contains(hostsInternal, host) {
hostsInternal = append(hostsInternal, host)
}
} else {
if !utils.StringInSlice(host, hostsDefaults) {
if !slices.Contains(hostsDefaults, host) {
hostsDefaults = append(hostsDefaults, host)
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/api/GetKubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ type GetClusterKubeconfigResult struct {
Token string
}

func (r *GetClusterKubeconfigResult) GetRawFileContent() ([]byte, error) {
func (r *GetClusterKubeconfigResult) GetRawFileContent(ctx context.Context) ([]byte, error) {
ctx, span := telemetry.Start(ctx, "api.GetRawFileContent")
defer span.End()

kubeConfig := `apiVersion: v1
clusters:
- cluster:
Expand All @@ -53,7 +56,7 @@ users:
user:
token: "{{ .Token }}"`

result, err := utils.GetTemplatedResult(kubeConfig, r)
result, err := utils.GetTemplatedResult(ctx, kubeConfig, r)
if err != nil {
return nil, errors.Wrap(err, "error getting templated string")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/GetRemoteServers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func GetRemoteServers(ctx context.Context) ([]*GetRemoteServerItem, error) {
Description: link.Description,
}

urlFormatted, err := utils.GetTemplatedResult(link.URL, item)
urlFormatted, err := utils.GetTemplatedResult(ctx, link.URL, item)
if err != nil {
log.WithError(err).Errorf("error parsing link %s", link.URL)
links[id].URL = link.URL
Expand Down
17 changes: 17 additions & 0 deletions pkg/api/ScaleDownDelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ package api

import (
"context"
"fmt"
"time"

"github.com/maksim-paskal/kubernetes-manager/pkg/config"
"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/maksim-paskal/kubernetes-manager/pkg/types"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/maksim-paskal/kubernetes-manager/pkg/webhook"
log "github.com/sirupsen/logrus"
)

func (e *Environment) ScaleDownDelay(ctx context.Context, durationTime time.Duration) error {
Expand All @@ -37,5 +41,18 @@ func (e *Environment) ScaleDownDelay(ctx context.Context, durationTime time.Dura
return err
}

err = webhook.NewEvent(ctx, types.WebhookMessage{
Event: types.EventPrestop,
Namespace: e.Namespace,
Cluster: e.Cluster,
Reason: fmt.Sprintf("Delayed for %s ...", durationTime.String()),
Properties: map[string]string{
"slackEmoji": ":calendar:",
},
})
if err != nil {
log.WithError(err).Error("error while sending webhook")
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/api/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (e *Environment) loadFromNamespace(ctx context.Context, namespace corev1.Na

// namespace description
if description, ok := e.NamespaceAnnotations[config.LabelDescription]; ok {
namespaceDescription, err := utils.GetTemplatedResult(description, e)
namespaceDescription, err := utils.GetTemplatedResult(ctx, description, e)
if err != nil {
return errors.Wrap(err, "can not parse description")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/getGitlabProjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ package api

import (
"context"
"slices"
"sort"
"strconv"

"github.com/maksim-paskal/kubernetes-manager/pkg/client"
"github.com/maksim-paskal/kubernetes-manager/pkg/config"
"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/pkg/errors"
"github.com/xanzy/go-gitlab"
)
Expand Down Expand Up @@ -105,10 +105,10 @@ func GetGitlabProjects(ctx context.Context, profile string, namespace string) ([
SelectedBranch: projectProfile.GetProjectSelectedBranch(project.ID),
}

exclude := utils.StringInSlice(strconv.Itoa(item.ProjectID), exludeProjects)
exclude := slices.Contains(exludeProjects, strconv.Itoa(item.ProjectID))

// if project in includes it must be always shown
if utils.StringInSlice(strconv.Itoa(item.ProjectID), includeProjects) {
if slices.Contains(includeProjects, strconv.Itoa(item.ProjectID)) {
exclude = false
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/getServices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ package api
import (
"context"
"fmt"
"slices"
"strconv"
"strings"

"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ func getFilteredLabels(labels map[string]string) string {
result := make([]string, 0)

for key, value := range labels {
if utils.StringInSlice(key, allowedServiceLabels) {
if slices.Contains(allowedServiceLabels, key) {
result = append(result, fmt.Sprintf("%s=%s", key, value))
}
}
Expand Down
33 changes: 26 additions & 7 deletions pkg/api/scaleALL.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,41 @@ func (e *Environment) ScaleALL(ctx context.Context, replicas int32) error {
ctx, span := telemetry.Start(ctx, "api.ScaleALL")
defer span.End()

// if scaleDown do nothing if environment have not pods
if replicas == 0 {
podInfo, err := e.GetPodsInfo(ctx)
if err != nil {
return errors.Wrap(err, "error while getting pods info")
}

if podInfo.PodsTotal == 0 {
return nil
}
}

processWebhook := make(chan error)
processScale := make(chan error)

go func() {
eventType := types.EventStart
eventReason := "Starting ..."
eventEmoji := ":white_check_mark:"

if replicas == 0 {
eventType = types.EventStop
eventReason = "Stopping ..."
eventEmoji = ":no_entry:"
}

processWebhook <- webhook.NewEvent(
ctx,
types.WebhookMessage{
Event: eventType,
Namespace: e.Namespace,
Cluster: e.Cluster,
})
processWebhook <- webhook.NewEvent(ctx, types.WebhookMessage{
Event: eventType,
Namespace: e.Namespace,
Cluster: e.Cluster,
Reason: eventReason,
Properties: map[string]string{
"slackEmoji": eventEmoji,
},
})
}()

go func() {
Expand Down
27 changes: 21 additions & 6 deletions pkg/api/startNewEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package api
import (
"context"
"fmt"
"slices"
"strconv"
"strings"

Expand All @@ -32,11 +33,22 @@ import (
type StartNewEnvironmentInput struct {
Profile string
Services string
User string
Cluster string
Name string
}

func (input *StartNewEnvironmentInput) GetUser(ctx context.Context) string {
ctx, span := telemetry.Start(ctx, "api.GetUser")
defer span.End()

security, ok := ctx.Value(types.ContextSecurityKey).(types.ContextSecurity)
if ok {
return security.Owner
}

return ""
}

func (input *StartNewEnvironmentInput) GetNamespace() (string, error) {
namespace, err := GetNamespaceByServices(input.GetProfile(), input.Services)
if err != nil {
Expand All @@ -55,7 +67,10 @@ func (input *StartNewEnvironmentInput) GetID() (string, error) {
return fmt.Sprintf("%s:%s", input.Cluster, namespace), nil
}

func (input *StartNewEnvironmentInput) Validation() error {
func (input *StartNewEnvironmentInput) Validation(ctx context.Context) error {
ctx, span := telemetry.Start(ctx, "api.Validation")
defer span.End()

if len(input.Cluster) == 0 {
return errors.Wrap(errCreateNewBranchMissingInput, "cluster is required")
}
Expand All @@ -64,7 +79,7 @@ func (input *StartNewEnvironmentInput) Validation() error {
return errors.Wrap(errCreateNewBranchMissingInput, "services is required")
}

if len(input.User) == 0 {
if len(input.GetUser(ctx)) == 0 {
return errors.Wrap(errCreateNewBranchMissingInput, "user is required")
}

Expand All @@ -91,7 +106,7 @@ func (input *StartNewEnvironmentInput) Validation() error {
}

for _, required := range input.GetProfile().GetRequired() {
if !utils.StringInSlice(required, selectedProjectIDs) {
if !slices.Contains(selectedProjectIDs, required) {
return errors.Wrapf(errCreateNewBranchMissingInput, "required service is missing")
}
}
Expand Down Expand Up @@ -155,7 +170,7 @@ func processCreateNewBranch(ctx context.Context, input *StartNewEnvironmentInput
ctx, span := telemetry.Start(ctx, "api.processCreateNewBranch")
defer span.End()

if err := input.Validation(); err != nil {
if err := input.Validation(ctx); err != nil {
return nil, errors.Wrap(err, "error validating")
}

Expand Down Expand Up @@ -258,7 +273,7 @@ func NewEnvironment(ctx context.Context, input *StartNewEnvironmentInput) (*Envi

namespace.ObjectMeta.Labels[config.Namespace] = config.TrueValue

creatorLabel := fmt.Sprintf("%s-%s", config.LabelNamespaceCreator, input.User)
creatorLabel := fmt.Sprintf("%s-%s", config.LabelNamespaceCreator, input.GetUser(ctx))
namespace.ObjectMeta.Labels[creatorLabel] = config.TrueValue

_, err = clientset.CoreV1().Namespaces().Create(ctx, &namespace, metav1.CreateOptions{})
Expand Down
11 changes: 7 additions & 4 deletions pkg/api/startNewEnvironment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ limitations under the License.
package api_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/maksim-paskal/kubernetes-manager/pkg/api"
"github.com/maksim-paskal/kubernetes-manager/pkg/config"
"github.com/maksim-paskal/kubernetes-manager/pkg/types"
)

func TestValidation(t *testing.T) {
Expand All @@ -33,19 +35,20 @@ func TestValidation(t *testing.T) {
valid = append(valid, api.StartNewEnvironmentInput{
Profile: "test",
Services: "1:test;2:test2;3:test3",
User: "test",
Cluster: "test",

Cluster: "test",
})

valid = append(valid, api.StartNewEnvironmentInput{
Profile: "test",
Services: "1:test",
User: "test1",
Cluster: "test2",
})

ctx := context.WithValue(context.Background(), types.ContextSecurityKey, types.ContextSecurity{Owner: "test"})

for _, input := range valid {
if err := input.Validation(); err != nil {
if err := input.Validation(ctx); err != nil {
t.Fatal(err)
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/maksim-paskal/kubernetes-manager/pkg/api"
"github.com/maksim-paskal/kubernetes-manager/pkg/config"
"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/maksim-paskal/kubernetes-manager/pkg/types"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/maksim-paskal/kubernetes-manager/pkg/webhook"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
Expand All @@ -31,6 +33,10 @@ func Schedule(ctx context.Context) {
ctx, span := telemetry.Start(ctx, "batch.Schedule")
defer span.End()

ctx = context.WithValue(ctx, types.ContextSecurityKey, types.ContextSecurity{
Owner: "BatchOperations",
})

ticker := time.NewTicker(config.Get().GetBatchShedulePeriod())

for ctx.Err() == nil {
Expand Down Expand Up @@ -80,6 +86,22 @@ func scaleDownALL(ctx context.Context) error {
return errors.Wrap(err, "error reload environment")
}

if environment.NeedToScaleDown(time.Now(), 1) {
message := types.WebhookMessage{
Event: types.EventPrestop,
Namespace: environment.Namespace,
Cluster: environment.Cluster,
Reason: "Will be scaled down soon...",
Properties: map[string]string{
"slackEmoji": ":warning:",
},
}

if err := webhook.NewEvent(ctx, message); err != nil {
log.WithError(err).Error()
}
}

if !environment.NeedToScaleDown(time.Now(), 0) {
return nil
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strconv"
"strings"
"time"

"github.com/maksim-paskal/kubernetes-manager/pkg/types"
"github.com/maksim-paskal/kubernetes-manager/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -157,7 +159,7 @@ func (p *ProjectProfile) GetInclude() []string {
}

func (p *ProjectProfile) IsProjectRequired(projectID int) bool {
return utils.StringInSlice(strconv.Itoa(projectID), p.GetRequired())
return slices.Contains(p.GetRequired(), strconv.Itoa(projectID))
}

func (p *ProjectProfile) GetProjectSelectedBranch(projectID int) string {
Expand Down Expand Up @@ -188,10 +190,10 @@ type NamespaceMeta struct {
}

type WebHook struct {
Provider string
Config interface{}
Cluster string
Namespace string
Provider string
Config interface{}
IDS []string
Events []types.Event
}

type Snapshot struct {
Expand Down
Loading
Loading