-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: network chaos and restart agent
- Loading branch information
Showing
10 changed files
with
348 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package cleanup | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/alibaba/ilogtail/test/engine/setup" | ||
) | ||
|
||
type ChaosStatus struct { | ||
Code int `json:"code"` | ||
Success bool `json:"success"` | ||
Result []map[string]string `json:"result"` | ||
} | ||
|
||
func DestoryAllChaos(ctx context.Context) (context.Context, error) { | ||
switch setup.Env.GetType() { | ||
case "host": | ||
command := "/opt/chaosblade/blade status --type create --status Success" | ||
response, err := setup.Env.ExecOnLogtail(command) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
var status ChaosStatus | ||
if err = json.Unmarshal([]byte(response), &status); err != nil { | ||
return ctx, err | ||
} | ||
for _, result := range status.Result { | ||
command = "/opt/chaosblade/blade destroy " + result["Uid"] | ||
setup.Env.ExecOnLogtail(command) | ||
} | ||
case "daemonset", "deployment": | ||
k8sEnv := setup.Env.(*setup.K8sEnv) | ||
chaosDir := filepath.Join("test_cases", "chaos") | ||
err := filepath.Walk(chaosDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
if filepath.Ext(path) != ".yaml" { | ||
return nil | ||
} | ||
return k8sEnv.Delete(path[len("test_cases/"):]) | ||
}) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
// delete chaosDir | ||
if err = os.RemoveAll(chaosDir); err != nil { | ||
return ctx, err | ||
} | ||
} | ||
return ctx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package control | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/alibaba/ilogtail/test/config" | ||
"github.com/alibaba/ilogtail/test/engine/setup" | ||
) | ||
|
||
func RestartAgent(ctx context.Context) (context.Context, error) { | ||
setup.Env.ExecOnLogtail("/etc/init.d/loongcollectord restart") | ||
return setup.SetAgentPID(ctx) | ||
} | ||
|
||
func ForceRestartAgent(ctx context.Context) (context.Context, error) { | ||
currentPID := ctx.Value(config.AgentPIDKey) | ||
if currentPID != nil { | ||
currentPIDs := strings.Split(currentPID.(string), "\n") | ||
for _, pid := range currentPIDs { | ||
setup.Env.ExecOnLogtail("kill -9 " + pid) | ||
} | ||
} | ||
fmt.Println("No agent pid found, skip force restart") | ||
setup.Env.ExecOnLogtail("/etc/init.d/loongcollectord start") | ||
return setup.SetAgentPID(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package chaos | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"text/template" | ||
|
||
"github.com/alibaba/ilogtail/test/engine/setup" | ||
) | ||
|
||
const ( | ||
// networkDelayCRD | ||
networkDelayCRDTmpl = ` | ||
apiVersion: chaosblade.io/v1alpha1 | ||
kind: ChaosBlade | ||
metadata: | ||
name: delay-pod-network | ||
spec: | ||
experiments: | ||
- scope: pod | ||
target: network | ||
action: delay | ||
desc: "delay pod network" | ||
matchers: | ||
- name: labels | ||
value: ["{{.PodLabel}}"] | ||
- name: namespace | ||
value: ["kube-system"] | ||
- name: interface | ||
value: ["eth0"] | ||
- name: destination-ip | ||
value: ["{{.Percent}}"] | ||
- name: time | ||
value: ["{{.Time}}"] | ||
` | ||
|
||
// networkLossCRD | ||
networkLossCRDTmpl = ` | ||
apiVersion: chaosblade.io/v1alpha1 | ||
kind: ChaosBlade | ||
metadata: | ||
name: loss-pod-network | ||
spec: | ||
experiments: | ||
- scope: pod | ||
target: network | ||
action: loss | ||
desc: "loss pod network" | ||
matchers: | ||
- name: labels | ||
value: ["{{.PodLabel}}"] | ||
- name: namespace | ||
value: ["kube-system"] | ||
- name: interface | ||
value: ["eth0"] | ||
- name: percent | ||
value: ["{{.Percent}}"] | ||
- name: exclude-port | ||
value: ["22"] | ||
- name: destination-ip | ||
value: ["{{.Ip}}"] | ||
` | ||
) | ||
|
||
func NetworkDelay(ctx context.Context, time int, ip string) (context.Context, error) { | ||
switch setup.Env.GetType() { | ||
case "host": | ||
command := "/opt/chaosblade/blade create network delay --time " + strconv.FormatInt(int64(time), 10) + " --exclude-port 22 --interface eth0 --destination-ip " + ip | ||
_, err := setup.Env.ExecOnLogtail(command) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
case "daemonset", "deployment": | ||
dir := filepath.Join("test_cases", "chaos") | ||
filename := "loss-pod-network.yaml" | ||
_ = os.Mkdir(dir, 0755) | ||
file, err := os.OpenFile(filepath.Join(dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) //nolint:gosec | ||
if err != nil { | ||
return ctx, err | ||
} | ||
defer file.Close() //nolint:gosec | ||
|
||
networkDelayCRD, _ := template.New("networkDelay").Parse(networkDelayCRDTmpl) | ||
if err = networkDelayCRD.Execute(file, map[string]string{ | ||
"PodLabel": getLoongCollectorPodLabel(), | ||
"Time": strconv.FormatInt(int64(time), 10), | ||
"Ip": ip, | ||
}); err != nil { | ||
return ctx, err | ||
} | ||
k8sEnv := setup.Env.(*setup.K8sEnv) | ||
if err := k8sEnv.Apply(filepath.Join("chaos", filename)); err != nil { | ||
return ctx, err | ||
} | ||
} | ||
return ctx, nil | ||
} | ||
|
||
func NetworkLoss(ctx context.Context, percentage int, ip string) (context.Context, error) { | ||
switch setup.Env.GetType() { | ||
case "host": | ||
command := "/opt/chaosblade/blade create network loss --percent " + strconv.FormatInt(int64(percentage), 10) + " --exclude-port 22 --interface eth0 --destination-ip " + ip | ||
_, err := setup.Env.ExecOnLogtail(command) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
case "daemonset", "deployment": | ||
dir := filepath.Join("test_cases", "chaos") | ||
filename := "loss-pod-network.yaml" | ||
_ = os.Mkdir(dir, 0755) | ||
file, err := os.OpenFile(filepath.Join(dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) //nolint:gosec | ||
if err != nil { | ||
return ctx, err | ||
} | ||
defer file.Close() | ||
|
||
networkLossCRD, _ := template.New("networkLoss").Parse(networkLossCRDTmpl) | ||
if err = networkLossCRD.Execute(file, map[string]string{ | ||
"PodLabel": getLoongCollectorPodLabel(), | ||
"Percent": strconv.FormatInt(int64(percentage), 10), | ||
"Ip": ip, | ||
}); err != nil { | ||
return ctx, err | ||
} | ||
k8sEnv := setup.Env.(*setup.K8sEnv) | ||
if err := k8sEnv.Apply(filepath.Join("chaos", filename)); err != nil { | ||
return ctx, err | ||
} | ||
} | ||
return ctx, nil | ||
} | ||
|
||
func getLoongCollectorPodLabel() string { | ||
var PodLabel string | ||
if setup.Env.GetType() == "daemonset" { | ||
PodLabel = "k8s-app=logtail-ds" | ||
} else if setup.Env.GetType() == "deployment" { | ||
PodLabel = "k8s-app=loongcollector-cluster" | ||
} | ||
return PodLabel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.