0

I am receiving a WatchEvent message and the value stored in this kubernetes proto is -

  1. Type - for ex. event such as modified or added or deleted
  2. Object - raw bytes of actual object. Can be any object.

I want to convert those raw bytes to actual objects such as pod. Note that I know the kind name of that RawExtension runtime object.

Currently I was doing this but result is negative -

  if( eventType == "ADDED" || eventType == "MODIFIED" )
  {
    WatchEvent watch;
    watch.set_type( eventType );
    string object = responseJson["object"].dump();
    watch.mutable_object()->set_raw(object);
    cout << "Message" << endl;
    cout << watch.DebugString() << endl;

    Pod pod;
    JsonStringToMessage( watch.object().raw(), &pod );
    cout << "Pod" << endl;
    cout << pod.DebugString() << endl;
  }
  • Have a look at std::bitcast (C++20) and std::start_lifetime_as (C++23). Note that direct casting (even with these casts) will be very fragile and rely on the fact that the sender and receiver expect the exact same memory layout. Which depends on system architecture and/or compiler settings. So I would not recommend doing that, just make a proto message for the actual data you want to transmit (and add it the the watchEvent response) and let serialization/deserialization do its job. – Pepijn Kramer Aug 07 '23 at 08:49
  • okay, will try it – Sumit Patil Aug 07 '23 at 08:51

0 Answers0