...

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 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package chaosdaemon
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"github.com/golang/protobuf/ptypes/empty"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/bpm"
    25  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    26  )
    27  
    28  const (
    29  	// DNSServerConfFile is the default config file for DNS server
    30  	DNSServerConfFile = "/etc/resolv.conf"
    31  )
    32  
    33  func (s *DaemonServer) SetDNSServer(ctx context.Context,
    34  	req *pb.SetDNSServerRequest) (*empty.Empty, error) {
    35  	log.Info("SetDNSServer", "request", req)
    36  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    37  	if err != nil {
    38  		log.Error(err, "GetPidFromContainerID")
    39  		return nil, err
    40  	}
    41  
    42  	if req.Enable {
    43  		// set dns server to the chaos dns server's address
    44  
    45  		if len(req.DnsServer) == 0 {
    46  			return &empty.Empty{}, fmt.Errorf("invalid set dns server request %v", req)
    47  		}
    48  
    49  		// backup the /etc/resolv.conf
    50  		processBuilder := bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("ls %s.chaos.bak || cp %s %s.chaos.bak", DNSServerConfFile, DNSServerConfFile, DNSServerConfFile)).SetContext(ctx)
    51  		if req.EnterNS {
    52  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    53  		}
    54  
    55  		cmd := processBuilder.Build()
    56  		output, err := cmd.CombinedOutput()
    57  		if err != nil {
    58  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    59  			return nil, encodeOutputToError(output, err)
    60  		}
    61  		if len(output) != 0 {
    62  			log.Info("command output", "output", string(output))
    63  		}
    64  
    65  		// add chaos dns server to the first line of /etc/resolv.conf
    66  		// Note: can not replace the /etc/resolv.conf like `mv temp resolv.conf`, will execute with error `Device or resource busy`
    67  		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)
    68  		if req.EnterNS {
    69  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    70  		}
    71  
    72  		cmd = processBuilder.Build()
    73  		output, err = cmd.CombinedOutput()
    74  		if err != nil {
    75  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    76  			return nil, encodeOutputToError(output, err)
    77  		}
    78  		if len(output) != 0 {
    79  			log.Info("command output", "output", string(output))
    80  		}
    81  	} else {
    82  		// recover the dns server's address
    83  		processBuilder := bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("ls %s.chaos.bak && cat %s.chaos.bak > %s || true", DNSServerConfFile, DNSServerConfFile, DNSServerConfFile)).SetContext(ctx)
    84  		if req.EnterNS {
    85  			processBuilder = processBuilder.SetNS(pid, bpm.MountNS)
    86  		}
    87  
    88  		cmd := processBuilder.Build()
    89  		output, err := cmd.CombinedOutput()
    90  		if err != nil {
    91  			log.Error(err, "execute command error", "command", cmd.String(), "output", output)
    92  			return nil, encodeOutputToError(output, err)
    93  		}
    94  		if len(output) != 0 {
    95  			log.Info("command output", "output", string(output))
    96  		}
    97  	}
    98  
    99  	return &empty.Empty{}, nil
   100  }
   101