...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/dns_server.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package chaosdaemon
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/golang/protobuf/ptypes/empty"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/pkg/bpm"
    23  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    24  )
    25  
    26  const (
    27  	// DNSServerConfFile is the default config file for DNS server
    28  	DNSServerConfFile = "/etc/resolv.conf"
    29  )
    30  
    31  func (s *DaemonServer) SetDNSServer(ctx context.Context,
    32  	req *pb.SetDNSServerRequest) (*empty.Empty, error) {
    33  	log.Info("SetDNSServer", "request", req)
    34  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    35  	if err != nil {
    36  		log.Error(err, "GetPidFromContainerID")
    37  		return nil, err
    38  	}
    39  
    40  	if req.Enable {
    41  		// set dns server to the chaos dns server's address
    42  
    43  		if len(req.DnsServer) == 0 {
    44  			return &empty.Empty{}, fmt.Errorf("invalid set dns server request %v", req)
    45  		}
    46  
    47  		// backup the /etc/resolv.conf
    48  		processBuilder := bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("ls %s.chaos.bak || cp %s %s.chaos.bak", DNSServerConfFile, DNSServerConfFile, DNSServerConfFile)).SetContext(ctx)
    49  		if req.EnterNS {
    50  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    51  		}
    52  
    53  		cmd := processBuilder.Build()
    54  		output, err := cmd.CombinedOutput()
    55  		if err != nil {
    56  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    57  			return nil, encodeOutputToError(output, err)
    58  		}
    59  		if len(output) != 0 {
    60  			log.Info("command output", "output", string(output))
    61  		}
    62  
    63  		// add chaos dns server to the first line of /etc/resolv.conf
    64  		// Note: can not replace the /etc/resolv.conf like `mv temp resolv.conf`, will execute with error `Device or resource busy`
    65  		processBuilder = bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("cp %s temp && sed -i 's/.*nameserver.*/nameserver %s/' temp && cat temp > %s", DNSServerConfFile, req.DnsServer, DNSServerConfFile)).SetContext(ctx)
    66  		if req.EnterNS {
    67  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    68  		}
    69  
    70  		cmd = processBuilder.Build()
    71  		output, err = cmd.CombinedOutput()
    72  		if err != nil {
    73  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    74  			return nil, encodeOutputToError(output, err)
    75  		}
    76  		if len(output) != 0 {
    77  			log.Info("command output", "output", string(output))
    78  		}
    79  	} else {
    80  		// recover the dns server's address
    81  		processBuilder := bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("ls %s.chaos.bak && cat %s.chaos.bak > %s || true", DNSServerConfFile, DNSServerConfFile, DNSServerConfFile)).SetContext(ctx)
    82  		if req.EnterNS {
    83  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    84  		}
    85  
    86  		cmd := processBuilder.Build()
    87  		output, err := cmd.CombinedOutput()
    88  		if err != nil {
    89  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    90  			return nil, encodeOutputToError(output, err)
    91  		}
    92  		if len(output) != 0 {
    93  			log.Info("command output", "output", string(output))
    94  		}
    95  	}
    96  
    97  	return &empty.Empty{}, nil
    98  }
    99